-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathClient.ts
More file actions
261 lines (247 loc) · 11.4 KB
/
Copy pathClient.ts
File metadata and controls
261 lines (247 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// This file was auto-generated by Fern from our API Definition.
import type { BaseClientOptions, BaseRequestOptions } from "../../../../BaseClient.js";
import { type NormalizedClientOptionsWithAuth, normalizeClientOptionsWithAuth } from "../../../../BaseClient.js";
import { mergeHeaders, mergeOnlyDefinedHeaders } from "../../../../core/headers.js";
import * as core from "../../../../core/index.js";
import * as environments from "../../../../environments.js";
import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.js";
import * as errors from "../../../../errors/index.js";
import * as Intercom from "../../../index.js";
export declare namespace NotesClient {
export interface Options extends BaseClientOptions {}
export interface RequestOptions extends BaseRequestOptions {}
}
/**
* Everything about your Notes
*/
export class NotesClient {
protected readonly _options: NormalizedClientOptionsWithAuth<NotesClient.Options>;
constructor(options: NotesClient.Options = {}) {
this._options = normalizeClientOptionsWithAuth(options);
}
/**
* You can fetch a list of notes that are associated to a contact.
*
* @param {Intercom.ListContactNotesRequest} request
* @param {NotesClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Intercom.NotFoundError}
*
* @example
* await client.notes.list({
* contact_id: "contact_id"
* })
*/
public async list(
request: Intercom.ListContactNotesRequest,
requestOptions?: NotesClient.RequestOptions,
): Promise<core.Page<Intercom.Note, Intercom.NoteList>> {
const list = core.HttpResponsePromise.interceptFunction(
async (request: Intercom.ListContactNotesRequest): Promise<core.WithRawResponse<Intercom.NoteList>> => {
const { contact_id: contactId, page, per_page: perPage } = request;
const _queryParams: Record<string, string | string[] | object | object[] | null> = {};
if (page != null) {
_queryParams.page = page.toString();
}
if (perPage != null) {
_queryParams.per_page = perPage.toString();
}
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
mergeOnlyDefinedHeaders({ "Intercom-Version": requestOptions?.version }),
requestOptions?.headers,
);
const _response = await (this._options.fetcher ?? core.fetcher)({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
(await core.Supplier.get(this._options.environment)) ??
environments.IntercomEnvironment.UsProduction,
`contacts/${core.url.encodePathParam(contactId)}/notes`,
),
method: "GET",
headers: _headers,
queryParameters: { ..._queryParams, ...requestOptions?.queryParams },
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 20) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return { data: _response.body as Intercom.NoteList, rawResponse: _response.rawResponse };
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 404:
throw new Intercom.NotFoundError(_response.error.body as unknown, _response.rawResponse);
default:
throw new errors.IntercomError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(
_response.error,
_response.rawResponse,
"GET",
"/contacts/{contact_id}/notes",
);
},
);
let _offset = request?.page != null ? request?.page : 1;
const dataWithRawResponse = await list(request).withRawResponse();
return new core.Page<Intercom.Note, Intercom.NoteList>({
response: dataWithRawResponse.data,
rawResponse: dataWithRawResponse.rawResponse,
hasNextPage: (response) => (response?.data ?? []).length > 0,
getItems: (response) => response?.data ?? [],
loadPage: (_response) => {
_offset += 1;
return list(core.setObjectProperty(request, "page", _offset));
},
});
}
/**
* You can add a note to a single contact.
*
* @param {Intercom.CreateContactNoteRequest} request
* @param {NotesClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Intercom.NotFoundError}
*
* @example
* await client.notes.create({
* contact_id: "123",
* body: "Hello",
* admin_id: "123"
* })
*/
public create(
request: Intercom.CreateContactNoteRequest,
requestOptions?: NotesClient.RequestOptions,
): core.HttpResponsePromise<Intercom.Note> {
return core.HttpResponsePromise.fromPromise(this.__create(request, requestOptions));
}
private async __create(
request: Intercom.CreateContactNoteRequest,
requestOptions?: NotesClient.RequestOptions,
): Promise<core.WithRawResponse<Intercom.Note>> {
const { contact_id: contactId, ..._body } = request;
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
mergeOnlyDefinedHeaders({ "Intercom-Version": requestOptions?.version }),
requestOptions?.headers,
);
const _response = await (this._options.fetcher ?? core.fetcher)({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
(await core.Supplier.get(this._options.environment)) ??
environments.IntercomEnvironment.UsProduction,
`contacts/${core.url.encodePathParam(contactId)}/notes`,
),
method: "POST",
headers: _headers,
contentType: "application/json",
queryParameters: requestOptions?.queryParams,
requestType: "json",
body: _body,
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 20) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return { data: _response.body as Intercom.Note, rawResponse: _response.rawResponse };
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 404:
throw new Intercom.NotFoundError(_response.error.body as unknown, _response.rawResponse);
default:
throw new errors.IntercomError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/contacts/{contact_id}/notes");
}
/**
* You can fetch the details of a single note.
*
* @param {Intercom.FindNoteRequest} request
* @param {NotesClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Intercom.UnauthorizedError}
* @throws {@link Intercom.NotFoundError}
*
* @example
* await client.notes.find({
* note_id: 1
* })
*/
public find(
request: Intercom.FindNoteRequest,
requestOptions?: NotesClient.RequestOptions,
): core.HttpResponsePromise<Intercom.Note> {
return core.HttpResponsePromise.fromPromise(this.__find(request, requestOptions));
}
private async __find(
request: Intercom.FindNoteRequest,
requestOptions?: NotesClient.RequestOptions,
): Promise<core.WithRawResponse<Intercom.Note>> {
const { note_id: noteId } = request;
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
mergeOnlyDefinedHeaders({ "Intercom-Version": requestOptions?.version }),
requestOptions?.headers,
);
const _response = await (this._options.fetcher ?? core.fetcher)({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
(await core.Supplier.get(this._options.environment)) ??
environments.IntercomEnvironment.UsProduction,
`notes/${core.url.encodePathParam(noteId)}`,
),
method: "GET",
headers: _headers,
queryParameters: requestOptions?.queryParams,
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 20) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return { data: _response.body as Intercom.Note, rawResponse: _response.rawResponse };
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 401:
throw new Intercom.UnauthorizedError(
_response.error.body as Intercom.Error_,
_response.rawResponse,
);
case 404:
throw new Intercom.NotFoundError(_response.error.body as unknown, _response.rawResponse);
default:
throw new errors.IntercomError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/notes/{note_id}");
}
}