|
| 1 | +import { |
| 2 | + type RESTDeleteDetachSegmentContactData, |
| 3 | + type RESTDeleteSegmentData, |
| 4 | + type RESTGetListSegmentContactsData, |
| 5 | + type RESTGetListSegmentContactsQueryParams, |
| 6 | + type RESTGetListSegmentsData, |
| 7 | + type RESTGetListSegmentsQueryParams, |
| 8 | + type RESTGetSegmentData, |
| 9 | + type RESTPatchUpdateSegmentBody, |
| 10 | + type RESTPatchUpdateSegmentData, |
| 11 | + type RESTPostAttachSegmentContactBody, |
| 12 | + type RESTPostAttachSegmentContactData, |
| 13 | + type RESTPostCreateSegmentBody, |
| 14 | + type RESTPostCreateSegmentData, |
| 15 | + Routes, |
| 16 | + type Snowflake, |
| 17 | +} from '@rewritetoday/types'; |
| 18 | +import { BaseManager } from './base'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Segment resource operations. |
| 22 | + */ |
| 23 | +export class SegmentManager extends BaseManager { |
| 24 | + /** |
| 25 | + * Lists segments for a project. |
| 26 | + */ |
| 27 | + public async list(options?: RESTGetListSegmentsQueryParams) { |
| 28 | + return await this.rest.get<RESTGetListSegmentsData>( |
| 29 | + Routes.segments.list(options), |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates a segment for a project. |
| 35 | + */ |
| 36 | + public async create(options: RESTPostCreateSegmentBody) { |
| 37 | + return await this.rest.post<RESTPostCreateSegmentData>( |
| 38 | + Routes.segments.create(), |
| 39 | + options, |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Fetches a segment by id. |
| 45 | + */ |
| 46 | + public async get(id: Snowflake) { |
| 47 | + return await this.rest.get<RESTGetSegmentData>(Routes.segments.get(id)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Updates a segment by id. |
| 52 | + */ |
| 53 | + public async update(id: Snowflake, options: RESTPatchUpdateSegmentBody) { |
| 54 | + return await this.rest.patch<RESTPatchUpdateSegmentData>( |
| 55 | + Routes.segments.update(id), |
| 56 | + options, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Deletes a segment by id. |
| 62 | + */ |
| 63 | + public async delete(id: Snowflake) { |
| 64 | + return await this.rest.delete<RESTDeleteSegmentData>( |
| 65 | + Routes.segments.delete(id), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Lists contacts attached to a segment. |
| 71 | + */ |
| 72 | + public async contacts( |
| 73 | + id: Snowflake, |
| 74 | + options?: RESTGetListSegmentContactsQueryParams, |
| 75 | + ) { |
| 76 | + return await this.rest.get<RESTGetListSegmentContactsData>( |
| 77 | + Routes.segments.contacts.list(id, options), |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Attaches a contact to a segment. |
| 83 | + */ |
| 84 | + public async attach( |
| 85 | + id: Snowflake, |
| 86 | + options: RESTPostAttachSegmentContactBody, |
| 87 | + ) { |
| 88 | + return await this.rest.post<RESTPostAttachSegmentContactData>( |
| 89 | + Routes.segments.contacts.attach(id), |
| 90 | + options, |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Detaches a contact from a segment. |
| 96 | + */ |
| 97 | + public async detach(id: Snowflake, contactId: Snowflake) { |
| 98 | + return await this.rest.delete<RESTDeleteDetachSegmentContactData>( |
| 99 | + Routes.segments.contacts.detach(id, contactId), |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments