@postpeer/node 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/api-promise.js.map +1 -1
- package/client.d.mts +5 -5
- package/client.d.mts.map +1 -1
- package/client.d.ts +5 -5
- package/client.d.ts.map +1 -1
- package/client.js +16 -61
- package/client.js.map +1 -1
- package/client.mjs +18 -63
- package/client.mjs.map +1 -1
- package/core/api-promise.d.mts.map +1 -1
- package/core/api-promise.d.ts.map +1 -1
- package/core/api-promise.js.map +1 -1
- package/core/api-promise.mjs.map +1 -1
- package/core/error.d.mts.map +1 -1
- package/core/error.d.ts.map +1 -1
- package/core/error.js +2 -2
- package/core/error.js.map +1 -1
- package/core/error.mjs +2 -2
- package/core/error.mjs.map +1 -1
- package/error.js.map +1 -1
- package/index.d.mts +1 -1
- package/index.d.mts.map +1 -1
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js.map +1 -1
- package/index.mjs +1 -1
- package/index.mjs.map +1 -1
- package/internal/builtin-types.d.mts +5 -5
- package/internal/builtin-types.d.mts.map +1 -1
- package/internal/builtin-types.d.ts +5 -5
- package/internal/builtin-types.d.ts.map +1 -1
- package/internal/detect-platform.d.mts.map +1 -1
- package/internal/detect-platform.d.ts.map +1 -1
- package/internal/detect-platform.js.map +1 -1
- package/internal/detect-platform.mjs.map +1 -1
- package/internal/errors.js +5 -6
- package/internal/errors.js.map +1 -1
- package/internal/errors.mjs +5 -6
- package/internal/errors.mjs.map +1 -1
- package/internal/headers.js +2 -2
- package/internal/headers.js.map +1 -1
- package/internal/headers.mjs +2 -2
- package/internal/headers.mjs.map +1 -1
- package/internal/parse.d.mts.map +1 -1
- package/internal/parse.d.ts.map +1 -1
- package/internal/parse.js +1 -7
- package/internal/parse.js.map +1 -1
- package/internal/parse.mjs +1 -7
- package/internal/parse.mjs.map +1 -1
- package/internal/qs/utils.d.mts.map +1 -1
- package/internal/qs/utils.d.ts.map +1 -1
- package/internal/qs/utils.js +2 -1
- package/internal/qs/utils.js.map +1 -1
- package/internal/qs/utils.mjs +2 -1
- package/internal/qs/utils.mjs.map +1 -1
- package/internal/request-options.d.mts.map +1 -1
- package/internal/request-options.d.ts.map +1 -1
- package/internal/request-options.js.map +1 -1
- package/internal/request-options.mjs.map +1 -1
- package/internal/shim-types.d.mts.map +1 -1
- package/internal/shim-types.d.ts.map +1 -1
- package/internal/shims.d.mts.map +1 -1
- package/internal/shims.d.ts.map +1 -1
- package/internal/shims.js.map +1 -1
- package/internal/shims.mjs.map +1 -1
- package/internal/to-file.d.mts.map +1 -1
- package/internal/to-file.d.ts.map +1 -1
- package/internal/to-file.js.map +1 -1
- package/internal/to-file.mjs.map +1 -1
- package/internal/types.d.mts +11 -17
- package/internal/types.d.mts.map +1 -1
- package/internal/types.d.ts +11 -17
- package/internal/types.d.ts.map +1 -1
- package/internal/utils/log.d.mts.map +1 -1
- package/internal/utils/log.d.ts.map +1 -1
- package/internal/utils/log.js +2 -9
- package/internal/utils/log.js.map +1 -1
- package/internal/utils/log.mjs +2 -9
- package/internal/utils/log.mjs.map +1 -1
- package/internal/utils/query.js.map +1 -1
- package/internal/utils/query.mjs.map +1 -1
- package/internal/utils/uuid.d.mts.map +1 -1
- package/internal/utils/uuid.d.ts.map +1 -1
- package/internal/utils/uuid.js +4 -2
- package/internal/utils/uuid.js.map +1 -1
- package/internal/utils/uuid.mjs +4 -2
- package/internal/utils/uuid.mjs.map +1 -1
- package/package.json +1 -1
- package/resource.js.map +1 -1
- package/resources/connect/connect.d.mts +2 -2
- package/resources/connect/connect.d.mts.map +1 -1
- package/resources/connect/connect.d.ts +2 -2
- package/resources/connect/connect.d.ts.map +1 -1
- package/resources/connect/connect.js.map +1 -1
- package/resources/connect/connect.mjs.map +1 -1
- package/resources/connect/index.d.mts +1 -1
- package/resources/connect/index.d.mts.map +1 -1
- package/resources/connect/index.d.ts +1 -1
- package/resources/connect/index.d.ts.map +1 -1
- package/resources/connect/index.js +2 -0
- package/resources/connect/index.js.map +1 -1
- package/resources/connect/index.mjs +3 -1
- package/resources/connect/index.mjs.map +1 -1
- package/resources/connect/integrations.d.mts +1 -1
- package/resources/connect/integrations.d.mts.map +1 -1
- package/resources/connect/integrations.d.ts +1 -1
- package/resources/connect/integrations.d.ts.map +1 -1
- package/resources/health.d.mts +1 -1
- package/resources/health.d.mts.map +1 -1
- package/resources/health.d.ts +1 -1
- package/resources/health.d.ts.map +1 -1
- package/resources/index.d.mts +1 -1
- package/resources/index.d.mts.map +1 -1
- package/resources/index.d.ts +1 -1
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js +5 -0
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +6 -1
- package/resources/index.mjs.map +1 -1
- package/resources/media.d.mts.map +1 -1
- package/resources/media.d.ts.map +1 -1
- package/resources/platforms.d.mts.map +1 -1
- package/resources/platforms.d.ts.map +1 -1
- package/resources/posts/index.d.mts +2 -2
- package/resources/posts/index.d.mts.map +1 -1
- package/resources/posts/index.d.ts +2 -2
- package/resources/posts/index.d.ts.map +1 -1
- package/resources/posts/index.js +2 -0
- package/resources/posts/index.js.map +1 -1
- package/resources/posts/index.mjs +4 -2
- package/resources/posts/index.mjs.map +1 -1
- package/resources/posts/posts.d.mts +97 -262
- package/resources/posts/posts.d.mts.map +1 -1
- package/resources/posts/posts.d.ts +97 -262
- package/resources/posts/posts.d.ts.map +1 -1
- package/resources/posts/posts.js.map +1 -1
- package/resources/posts/posts.mjs +1 -1
- package/resources/posts/posts.mjs.map +1 -1
- package/resources/posts/scheduled.d.mts +3 -9
- package/resources/posts/scheduled.d.mts.map +1 -1
- package/resources/posts/scheduled.d.ts +3 -9
- package/resources/posts/scheduled.d.ts.map +1 -1
- package/resources/posts/scheduled.js.map +1 -1
- package/resources/posts/scheduled.mjs.map +1 -1
- package/src/api-promise.ts +1 -1
- package/src/client.ts +76 -167
- package/src/core/api-promise.ts +2 -7
- package/src/core/error.ts +24 -22
- package/src/error.ts +1 -1
- package/src/index.ts +1 -15
- package/src/internal/builtin-types.ts +9 -14
- package/src/internal/detect-platform.ts +1 -5
- package/src/internal/errors.ts +7 -7
- package/src/internal/headers.ts +2 -2
- package/src/internal/parse.ts +2 -10
- package/src/internal/qs/utils.ts +4 -1
- package/src/internal/request-options.ts +5 -1
- package/src/internal/shim-types.ts +2 -2
- package/src/internal/shims.ts +1 -3
- package/src/internal/to-file.ts +7 -7
- package/src/internal/types.ts +22 -28
- package/src/internal/utils/log.ts +8 -30
- package/src/internal/utils/query.ts +1 -1
- package/src/internal/utils/uuid.ts +5 -3
- package/src/resource.ts +1 -1
- package/src/resources/connect/connect.ts +3 -7
- package/src/resources/connect/index.ts +2 -6
- package/src/resources/connect/integrations.ts +1 -1
- package/src/resources/health.ts +1 -1
- package/src/resources/index.ts +5 -15
- package/src/resources/media.ts +4 -1
- package/src/resources/platforms.ts +3 -1
- package/src/resources/posts/index.ts +2 -18
- package/src/resources/posts/posts.ts +138 -340
- package/src/resources/posts/scheduled.ts +4 -14
- package/src/uploads.ts +1 -1
- package/src/version.ts +1 -1
- package/uploads.js.map +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.js.map +1 -1
- package/version.mjs +1 -1
- package/version.mjs.map +1 -1
package/src/client.ts
CHANGED
|
@@ -21,28 +21,12 @@ import { Health, HealthCheckResponse, HealthVerifyAccessKeyResponse } from './re
|
|
|
21
21
|
import { Media, MediaUploadParams, MediaUploadResponse } from './resources/media';
|
|
22
22
|
import { PlatformListResponse, Platforms } from './resources/platforms';
|
|
23
23
|
import { Connect, ConnectGetOAuthURLParams, ConnectGetOAuthURLResponse } from './resources/connect/connect';
|
|
24
|
-
import {
|
|
25
|
-
PostCreateParams,
|
|
26
|
-
PostCreateResponse,
|
|
27
|
-
PostDeleteResponse,
|
|
28
|
-
PostListParams,
|
|
29
|
-
PostListResponse,
|
|
30
|
-
PostRetrieveResponse,
|
|
31
|
-
PostSummary,
|
|
32
|
-
Posts,
|
|
33
|
-
Status,
|
|
34
|
-
} from './resources/posts/posts';
|
|
24
|
+
import { PostCreateParams, PostCreateResponse, PostDeleteResponse, PostListParams, PostListResponse, PostRetrieveResponse, Posts, Status } from './resources/posts/posts';
|
|
35
25
|
import { type Fetch } from './internal/builtin-types';
|
|
36
26
|
import { HeadersLike, NullableHeaders, buildHeaders } from './internal/headers';
|
|
37
27
|
import { FinalRequestOptions, RequestOptions } from './internal/request-options';
|
|
38
28
|
import { readEnv } from './internal/utils/env';
|
|
39
|
-
import {
|
|
40
|
-
type LogLevel,
|
|
41
|
-
type Logger,
|
|
42
|
-
formatRequestDetails,
|
|
43
|
-
loggerFor,
|
|
44
|
-
parseLogLevel,
|
|
45
|
-
} from './internal/utils/log';
|
|
29
|
+
import { type LogLevel, type Logger, formatRequestDetails, loggerFor, parseLogLevel } from './internal/utils/log';
|
|
46
30
|
import { isEmptyObj } from './internal/utils/values';
|
|
47
31
|
|
|
48
32
|
export interface ClientOptions {
|
|
@@ -121,7 +105,7 @@ export interface ClientOptions {
|
|
|
121
105
|
}
|
|
122
106
|
|
|
123
107
|
/**
|
|
124
|
-
* API Client for interfacing with the Postpeer API.
|
|
108
|
+
* API Client for interfacing with the Postpeer API.
|
|
125
109
|
*/
|
|
126
110
|
export class Postpeer {
|
|
127
111
|
apiKey: string;
|
|
@@ -157,7 +141,7 @@ export class Postpeer {
|
|
|
157
141
|
}: ClientOptions = {}) {
|
|
158
142
|
if (apiKey === undefined) {
|
|
159
143
|
throw new Errors.PostpeerError(
|
|
160
|
-
|
|
144
|
+
'The POSTPEER_API_KEY environment variable is missing or empty; either provide it, or instantiate the Postpeer client with an apiKey option, like new Postpeer({ apiKey: \'My API Key\' }).'
|
|
161
145
|
);
|
|
162
146
|
}
|
|
163
147
|
|
|
@@ -173,10 +157,7 @@ export class Postpeer {
|
|
|
173
157
|
const defaultLogLevel = 'warn';
|
|
174
158
|
// Set default logLevel early so that we can log a warning in parseLogLevel.
|
|
175
159
|
this.logLevel = defaultLogLevel;
|
|
176
|
-
this.logLevel =
|
|
177
|
-
parseLogLevel(options.logLevel, 'ClientOptions.logLevel', this) ??
|
|
178
|
-
parseLogLevel(readEnv('POSTPEER_LOG'), "process.env['POSTPEER_LOG']", this) ??
|
|
179
|
-
defaultLogLevel;
|
|
160
|
+
this.logLevel = parseLogLevel(options.logLevel, 'ClientOptions.logLevel', this) ?? parseLogLevel(readEnv('POSTPEER_LOG'), 'process.env[\'POSTPEER_LOG\']', this) ?? defaultLogLevel;
|
|
180
161
|
this.fetchOptions = options.fetchOptions;
|
|
181
162
|
this.maxRetries = options.maxRetries ?? 2;
|
|
182
163
|
this.fetch = options.fetch ?? Shims.getDefaultFetch();
|
|
@@ -201,7 +182,7 @@ export class Postpeer {
|
|
|
201
182
|
fetch: this.fetch,
|
|
202
183
|
fetchOptions: this.fetchOptions,
|
|
203
184
|
apiKey: this.apiKey,
|
|
204
|
-
...options
|
|
185
|
+
...options
|
|
205
186
|
});
|
|
206
187
|
return client;
|
|
207
188
|
}
|
|
@@ -214,7 +195,7 @@ export class Postpeer {
|
|
|
214
195
|
}
|
|
215
196
|
|
|
216
197
|
protected defaultQuery(): Record<string, string | undefined> | undefined {
|
|
217
|
-
return this._options.defaultQuery
|
|
198
|
+
return this._options.defaultQuery
|
|
218
199
|
}
|
|
219
200
|
|
|
220
201
|
protected validateHeaders({ values, nulls }: NullableHeaders) {
|
|
@@ -246,11 +227,7 @@ export class Postpeer {
|
|
|
246
227
|
return Errors.APIError.generate(status, error, message, headers);
|
|
247
228
|
}
|
|
248
229
|
|
|
249
|
-
buildURL(
|
|
250
|
-
path: string,
|
|
251
|
-
query: Record<string, unknown> | null | undefined,
|
|
252
|
-
defaultBaseURL?: string | undefined,
|
|
253
|
-
): string {
|
|
230
|
+
buildURL(path: string, query: Record<string, unknown> | null | undefined, defaultBaseURL?: string | undefined): string {
|
|
254
231
|
const baseURL = (!this.#baseURLOverridden() && defaultBaseURL) || this.baseURL;
|
|
255
232
|
const url =
|
|
256
233
|
isAbsoluteURL(path) ?
|
|
@@ -338,9 +315,7 @@ export class Postpeer {
|
|
|
338
315
|
|
|
339
316
|
await this.prepareOptions(options);
|
|
340
317
|
|
|
341
|
-
const { req, url, timeout } = await this.buildRequest(options, {
|
|
342
|
-
retryCount: maxRetries - retriesRemaining,
|
|
343
|
-
});
|
|
318
|
+
const { req, url, timeout } = await this.buildRequest(options, { retryCount: maxRetries - retriesRemaining });
|
|
344
319
|
|
|
345
320
|
await this.prepareRequest(req, { url, options });
|
|
346
321
|
|
|
@@ -349,16 +324,7 @@ export class Postpeer {
|
|
|
349
324
|
const retryLogStr = retryOfRequestLogID === undefined ? '' : `, retryOf: ${retryOfRequestLogID}`;
|
|
350
325
|
const startTime = Date.now();
|
|
351
326
|
|
|
352
|
-
loggerFor(this).debug(
|
|
353
|
-
`[${requestLogID}] sending request`,
|
|
354
|
-
formatRequestDetails({
|
|
355
|
-
retryOfRequestLogID,
|
|
356
|
-
method: options.method,
|
|
357
|
-
url,
|
|
358
|
-
options,
|
|
359
|
-
headers: req.headers,
|
|
360
|
-
}),
|
|
361
|
-
);
|
|
327
|
+
loggerFor(this).debug(`[${requestLogID}] sending request`, formatRequestDetails({ retryOfRequestLogID, method: options.method, url, options, headers: req.headers }));
|
|
362
328
|
|
|
363
329
|
if (options.signal?.aborted) {
|
|
364
330
|
throw new Errors.APIUserAbortError();
|
|
@@ -377,45 +343,21 @@ export class Postpeer {
|
|
|
377
343
|
// deno throws "TypeError: error sending request for url (https://example/): client error (Connect): tcp connect error: Operation timed out (os error 60): Operation timed out (os error 60)"
|
|
378
344
|
// undici throws "TypeError: fetch failed" with cause "ConnectTimeoutError: Connect Timeout Error (attempted address: example:443, timeout: 1ms)"
|
|
379
345
|
// others do not provide enough information to distinguish timeouts from other connection errors
|
|
380
|
-
const isTimeout =
|
|
381
|
-
isAbortError(response) ||
|
|
382
|
-
/timed? ?out/i.test(String(response) + ('cause' in response ? String(response.cause) : ''));
|
|
346
|
+
const isTimeout = isAbortError(response) || /timed? ?out/i.test(String(response) + ('cause' in response ? String(response.cause) : ''))
|
|
383
347
|
if (retriesRemaining) {
|
|
384
|
-
loggerFor(this).info(
|
|
385
|
-
|
|
386
|
-
);
|
|
387
|
-
loggerFor(this).debug(
|
|
388
|
-
`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (${retryMessage})`,
|
|
389
|
-
formatRequestDetails({
|
|
390
|
-
retryOfRequestLogID,
|
|
391
|
-
url,
|
|
392
|
-
durationMs: headersTime - startTime,
|
|
393
|
-
message: response.message,
|
|
394
|
-
}),
|
|
395
|
-
);
|
|
348
|
+
loggerFor(this).info(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - ${retryMessage}`)
|
|
349
|
+
loggerFor(this).debug(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (${retryMessage})`, formatRequestDetails({ retryOfRequestLogID, url, durationMs: headersTime - startTime, message: response.message }));
|
|
396
350
|
return this.retryRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID);
|
|
397
351
|
}
|
|
398
|
-
loggerFor(this).info(
|
|
399
|
-
|
|
400
|
-
);
|
|
401
|
-
loggerFor(this).debug(
|
|
402
|
-
`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (error; no more retries left)`,
|
|
403
|
-
formatRequestDetails({
|
|
404
|
-
retryOfRequestLogID,
|
|
405
|
-
url,
|
|
406
|
-
durationMs: headersTime - startTime,
|
|
407
|
-
message: response.message,
|
|
408
|
-
}),
|
|
409
|
-
);
|
|
352
|
+
loggerFor(this).info(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - error; no more retries left`)
|
|
353
|
+
loggerFor(this).debug(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (error; no more retries left)`, formatRequestDetails({ retryOfRequestLogID, url, durationMs: headersTime - startTime, message: response.message }));
|
|
410
354
|
if (isTimeout) {
|
|
411
355
|
throw new Errors.APIConnectionTimeoutError();
|
|
412
356
|
}
|
|
413
357
|
throw new Errors.APIConnectionError({ cause: response });
|
|
414
358
|
}
|
|
415
359
|
|
|
416
|
-
const responseInfo = `[${requestLogID}${retryLogStr}] ${req.method} ${url} ${
|
|
417
|
-
response.ok ? 'succeeded' : 'failed'
|
|
418
|
-
} with status ${response.status} in ${headersTime - startTime}ms`;
|
|
360
|
+
const responseInfo = `[${requestLogID}${retryLogStr}] ${req.method} ${url} ${response.ok ? 'succeeded' : 'failed'} with status ${response.status} in ${headersTime - startTime}ms`;
|
|
419
361
|
|
|
420
362
|
if (!response.ok) {
|
|
421
363
|
const shouldRetry = await this.shouldRetry(response);
|
|
@@ -424,60 +366,27 @@ export class Postpeer {
|
|
|
424
366
|
|
|
425
367
|
// We don't need the body of this response.
|
|
426
368
|
await Shims.CancelReadableStream(response.body);
|
|
427
|
-
loggerFor(this).info(`${responseInfo} - ${retryMessage}`)
|
|
428
|
-
loggerFor(this).debug(
|
|
429
|
-
|
|
430
|
-
formatRequestDetails({
|
|
431
|
-
retryOfRequestLogID,
|
|
432
|
-
url: response.url,
|
|
433
|
-
status: response.status,
|
|
434
|
-
headers: response.headers,
|
|
435
|
-
durationMs: headersTime - startTime,
|
|
436
|
-
}),
|
|
437
|
-
);
|
|
438
|
-
return this.retryRequest(
|
|
439
|
-
options,
|
|
440
|
-
retriesRemaining,
|
|
441
|
-
retryOfRequestLogID ?? requestLogID,
|
|
442
|
-
response.headers,
|
|
443
|
-
);
|
|
369
|
+
loggerFor(this).info(`${responseInfo} - ${retryMessage}`)
|
|
370
|
+
loggerFor(this).debug(`[${requestLogID}] response error (${retryMessage})`, formatRequestDetails({ retryOfRequestLogID, url: response.url, status: response.status, headers: response.headers, durationMs: headersTime - startTime }));
|
|
371
|
+
return this.retryRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID, response.headers);
|
|
444
372
|
}
|
|
445
373
|
|
|
446
374
|
const retryMessage = shouldRetry ? `error; no more retries left` : `error; not retryable`;
|
|
447
375
|
|
|
448
|
-
loggerFor(this).info(`${responseInfo} - ${retryMessage}`)
|
|
376
|
+
loggerFor(this).info(`${responseInfo} - ${retryMessage}`)
|
|
449
377
|
|
|
450
378
|
const errText = await response.text().catch((err: any) => castToError(err).message);
|
|
451
379
|
const errJSON = safeJSON(errText) as any;
|
|
452
380
|
const errMessage = errJSON ? undefined : errText;
|
|
453
381
|
|
|
454
|
-
loggerFor(this).debug(
|
|
455
|
-
`[${requestLogID}] response error (${retryMessage})`,
|
|
456
|
-
formatRequestDetails({
|
|
457
|
-
retryOfRequestLogID,
|
|
458
|
-
url: response.url,
|
|
459
|
-
status: response.status,
|
|
460
|
-
headers: response.headers,
|
|
461
|
-
message: errMessage,
|
|
462
|
-
durationMs: Date.now() - startTime,
|
|
463
|
-
}),
|
|
464
|
-
);
|
|
382
|
+
loggerFor(this).debug(`[${requestLogID}] response error (${retryMessage})`, formatRequestDetails({ retryOfRequestLogID, url: response.url, status: response.status, headers: response.headers, message: errMessage, durationMs: Date.now() - startTime }));
|
|
465
383
|
|
|
466
384
|
const err = this.makeStatusError(response.status, errJSON, errMessage, response.headers);
|
|
467
385
|
throw err;
|
|
468
386
|
}
|
|
469
387
|
|
|
470
|
-
loggerFor(this).info(responseInfo)
|
|
471
|
-
loggerFor(this).debug(
|
|
472
|
-
`[${requestLogID}] response start`,
|
|
473
|
-
formatRequestDetails({
|
|
474
|
-
retryOfRequestLogID,
|
|
475
|
-
url: response.url,
|
|
476
|
-
status: response.status,
|
|
477
|
-
headers: response.headers,
|
|
478
|
-
durationMs: headersTime - startTime,
|
|
479
|
-
}),
|
|
480
|
-
);
|
|
388
|
+
loggerFor(this).info(responseInfo)
|
|
389
|
+
loggerFor(this).debug(`[${requestLogID}] response start`, formatRequestDetails({ retryOfRequestLogID, url: response.url, status: response.status, headers: response.headers, durationMs: headersTime - startTime }));
|
|
481
390
|
|
|
482
391
|
return { response, options, controller, requestLogID, retryOfRequestLogID, startTime };
|
|
483
392
|
}
|
|
@@ -494,9 +403,7 @@ export class Postpeer {
|
|
|
494
403
|
|
|
495
404
|
const timeout = setTimeout(abort, ms);
|
|
496
405
|
|
|
497
|
-
const isReadableBody =
|
|
498
|
-
((globalThis as any).ReadableStream && options.body instanceof (globalThis as any).ReadableStream) ||
|
|
499
|
-
(typeof options.body === 'object' && options.body !== null && Symbol.asyncIterator in options.body);
|
|
406
|
+
const isReadableBody = ((globalThis as any).ReadableStream && options.body instanceof (globalThis as any).ReadableStream) || (typeof options.body === "object" && options.body !== null && Symbol.asyncIterator in options.body);
|
|
500
407
|
|
|
501
408
|
const fetchOptions: RequestInit = {
|
|
502
409
|
signal: controller.signal as any,
|
|
@@ -511,6 +418,7 @@ export class Postpeer {
|
|
|
511
418
|
}
|
|
512
419
|
|
|
513
420
|
try {
|
|
421
|
+
|
|
514
422
|
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
|
|
515
423
|
return await this.fetch.call(undefined, url, fetchOptions);
|
|
516
424
|
} finally {
|
|
@@ -611,12 +519,11 @@ export class Postpeer {
|
|
|
611
519
|
const req: FinalizedRequestInit = {
|
|
612
520
|
method,
|
|
613
521
|
headers: reqHeaders,
|
|
614
|
-
...(options.signal && { signal: options.signal
|
|
615
|
-
...((globalThis as any).ReadableStream &&
|
|
616
|
-
body instanceof (globalThis as any).ReadableStream && { duplex: 'half' }),
|
|
522
|
+
...(options.signal && { signal: options.signal}),
|
|
523
|
+
...((globalThis as any).ReadableStream && body instanceof (globalThis as any).ReadableStream && { duplex: "half" }),
|
|
617
524
|
...(body && { body }),
|
|
618
|
-
...(
|
|
619
|
-
...(
|
|
525
|
+
...(this.fetchOptions as any ?? {}),
|
|
526
|
+
...(options.fetchOptions as any ?? {}),
|
|
620
527
|
};
|
|
621
528
|
|
|
622
529
|
return { req, url, timeout: options.timeout };
|
|
@@ -641,17 +548,15 @@ export class Postpeer {
|
|
|
641
548
|
|
|
642
549
|
const headers = buildHeaders([
|
|
643
550
|
idempotencyHeaders,
|
|
644
|
-
{
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
...getPlatformHeaders(),
|
|
650
|
-
},
|
|
551
|
+
{Accept: 'application/json',
|
|
552
|
+
'User-Agent': this.getUserAgent(),
|
|
553
|
+
'X-Stainless-Retry-Count': String(retryCount),
|
|
554
|
+
...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),
|
|
555
|
+
...getPlatformHeaders()},
|
|
651
556
|
await this.authHeaders(options),
|
|
652
557
|
this._options.defaultHeaders,
|
|
653
558
|
bodyHeaders,
|
|
654
|
-
options.headers
|
|
559
|
+
options.headers
|
|
655
560
|
]);
|
|
656
561
|
|
|
657
562
|
this.validateHeaders(headers);
|
|
@@ -678,9 +583,11 @@ export class Postpeer {
|
|
|
678
583
|
ArrayBuffer.isView(body) ||
|
|
679
584
|
body instanceof ArrayBuffer ||
|
|
680
585
|
body instanceof DataView ||
|
|
681
|
-
(
|
|
586
|
+
(
|
|
587
|
+
typeof body === 'string' &&
|
|
682
588
|
// Preserve legacy string encoding behavior for now
|
|
683
|
-
headers.values.has('content-type')
|
|
589
|
+
headers.values.has('content-type')
|
|
590
|
+
) ||
|
|
684
591
|
// `Blob` is superset of `File`
|
|
685
592
|
((globalThis as any).Blob && body instanceof (globalThis as any).Blob) ||
|
|
686
593
|
// `FormData` -> `multipart/form-data`
|
|
@@ -711,7 +618,7 @@ export class Postpeer {
|
|
|
711
618
|
}
|
|
712
619
|
|
|
713
620
|
static Postpeer = this;
|
|
714
|
-
static DEFAULT_TIMEOUT = 60000
|
|
621
|
+
static DEFAULT_TIMEOUT = 60000 // 1 minute
|
|
715
622
|
|
|
716
623
|
static PostpeerError = Errors.PostpeerError;
|
|
717
624
|
static APIError = Errors.APIError;
|
|
@@ -743,37 +650,39 @@ Postpeer.Posts = Posts;
|
|
|
743
650
|
Postpeer.Media = Media;
|
|
744
651
|
|
|
745
652
|
export declare namespace Postpeer {
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
653
|
+
export type RequestOptions = Opts.RequestOptions;
|
|
654
|
+
|
|
655
|
+
export {
|
|
656
|
+
Health as Health,
|
|
657
|
+
type HealthCheckResponse as HealthCheckResponse,
|
|
658
|
+
type HealthVerifyAccessKeyResponse as HealthVerifyAccessKeyResponse
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
export {
|
|
662
|
+
Connect as Connect,
|
|
663
|
+
type ConnectGetOAuthURLResponse as ConnectGetOAuthURLResponse,
|
|
664
|
+
type ConnectGetOAuthURLParams as ConnectGetOAuthURLParams
|
|
665
|
+
};
|
|
666
|
+
|
|
667
|
+
export {
|
|
668
|
+
Platforms as Platforms,
|
|
669
|
+
type PlatformListResponse as PlatformListResponse
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
export {
|
|
673
|
+
Posts as Posts,
|
|
674
|
+
type Status as Status,
|
|
675
|
+
type PostCreateResponse as PostCreateResponse,
|
|
676
|
+
type PostRetrieveResponse as PostRetrieveResponse,
|
|
677
|
+
type PostListResponse as PostListResponse,
|
|
678
|
+
type PostDeleteResponse as PostDeleteResponse,
|
|
679
|
+
type PostCreateParams as PostCreateParams,
|
|
680
|
+
type PostListParams as PostListParams
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
export {
|
|
684
|
+
Media as Media,
|
|
685
|
+
type MediaUploadResponse as MediaUploadResponse,
|
|
686
|
+
type MediaUploadParams as MediaUploadParams
|
|
687
|
+
};
|
|
688
|
+
}
|
package/src/core/api-promise.ts
CHANGED
|
@@ -16,10 +16,7 @@ export class APIPromise<T> extends Promise<T> {
|
|
|
16
16
|
constructor(
|
|
17
17
|
client: Postpeer,
|
|
18
18
|
private responsePromise: Promise<APIResponseProps>,
|
|
19
|
-
private parseResponse: (
|
|
20
|
-
client: Postpeer,
|
|
21
|
-
props: APIResponseProps,
|
|
22
|
-
) => PromiseOrValue<T> = defaultParseResponse,
|
|
19
|
+
private parseResponse: (client: Postpeer, props: APIResponseProps) => PromiseOrValue<T> = defaultParseResponse,
|
|
23
20
|
) {
|
|
24
21
|
super((resolve) => {
|
|
25
22
|
// this is maybe a bit weird but this has to be a no-op to not implicitly
|
|
@@ -31,9 +28,7 @@ export class APIPromise<T> extends Promise<T> {
|
|
|
31
28
|
}
|
|
32
29
|
|
|
33
30
|
_thenUnwrap<U>(transform: (data: T, props: APIResponseProps) => U): APIPromise<U> {
|
|
34
|
-
return new APIPromise(this.#client, this.responsePromise, async (client, props) =>
|
|
35
|
-
transform(await this.parseResponse(client, props), props),
|
|
36
|
-
);
|
|
31
|
+
return new APIPromise(this.#client, this.responsePromise, async (client, props) => transform(await this.parseResponse(client, props), props));
|
|
37
32
|
}
|
|
38
33
|
|
|
39
34
|
/**
|
package/src/core/error.ts
CHANGED
|
@@ -2,13 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
import { castToError } from '../internal/errors';
|
|
4
4
|
|
|
5
|
-
export class PostpeerError extends Error {
|
|
5
|
+
export class PostpeerError extends Error {
|
|
6
|
+
}
|
|
6
7
|
|
|
7
|
-
export class APIError<
|
|
8
|
-
TStatus extends number | undefined = number | undefined,
|
|
9
|
-
THeaders extends Headers | undefined = Headers | undefined,
|
|
10
|
-
TError extends Object | undefined = Object | undefined,
|
|
11
|
-
> extends PostpeerError {
|
|
8
|
+
export class APIError<TStatus extends number | undefined = number | undefined, THeaders extends Headers | undefined = Headers | undefined, TError extends Object | undefined = Object | undefined> extends PostpeerError {
|
|
12
9
|
/** HTTP status for the response that caused the error */
|
|
13
10
|
readonly status: TStatus;
|
|
14
11
|
/** HTTP headers for the response that caused the error */
|
|
@@ -16,6 +13,9 @@ export class APIError<
|
|
|
16
13
|
/** JSON body of the response that caused the error */
|
|
17
14
|
readonly error: TError;
|
|
18
15
|
|
|
16
|
+
|
|
17
|
+
;
|
|
18
|
+
|
|
19
19
|
constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders) {
|
|
20
20
|
super(`${APIError.makeMessage(status, error, message)}`);
|
|
21
21
|
this.status = status;
|
|
@@ -26,8 +26,7 @@ export class APIError<
|
|
|
26
26
|
private static makeMessage(status: number | undefined, error: any, message: string | undefined) {
|
|
27
27
|
const msg =
|
|
28
28
|
error?.message ?
|
|
29
|
-
typeof error.message === 'string' ?
|
|
30
|
-
error.message
|
|
29
|
+
typeof error.message === 'string' ? error.message
|
|
31
30
|
: JSON.stringify(error.message)
|
|
32
31
|
: error ? JSON.stringify(error)
|
|
33
32
|
: message;
|
|
@@ -44,12 +43,7 @@ export class APIError<
|
|
|
44
43
|
return '(no status code or body)';
|
|
45
44
|
}
|
|
46
45
|
|
|
47
|
-
static generate(
|
|
48
|
-
status: number | undefined,
|
|
49
|
-
errorResponse: Object | undefined,
|
|
50
|
-
message: string | undefined,
|
|
51
|
-
headers: Headers | undefined,
|
|
52
|
-
): APIError {
|
|
46
|
+
static generate(status: number | undefined, errorResponse: Object | undefined, message: string | undefined, headers: Headers | undefined): APIError {
|
|
53
47
|
if (!status || !headers) {
|
|
54
48
|
return new APIConnectionError({ message, cause: castToError(errorResponse) });
|
|
55
49
|
}
|
|
@@ -113,18 +107,26 @@ export class APIConnectionTimeoutError extends APIConnectionError {
|
|
|
113
107
|
}
|
|
114
108
|
}
|
|
115
109
|
|
|
116
|
-
export class BadRequestError extends APIError<400, Headers> {
|
|
110
|
+
export class BadRequestError extends APIError<400, Headers> {
|
|
111
|
+
}
|
|
117
112
|
|
|
118
|
-
export class AuthenticationError extends APIError<401, Headers> {
|
|
113
|
+
export class AuthenticationError extends APIError<401, Headers> {
|
|
114
|
+
}
|
|
119
115
|
|
|
120
|
-
export class PermissionDeniedError extends APIError<403, Headers> {
|
|
116
|
+
export class PermissionDeniedError extends APIError<403, Headers> {
|
|
117
|
+
}
|
|
121
118
|
|
|
122
|
-
export class NotFoundError extends APIError<404, Headers> {
|
|
119
|
+
export class NotFoundError extends APIError<404, Headers> {
|
|
120
|
+
}
|
|
123
121
|
|
|
124
|
-
export class ConflictError extends APIError<409, Headers> {
|
|
122
|
+
export class ConflictError extends APIError<409, Headers> {
|
|
123
|
+
}
|
|
125
124
|
|
|
126
|
-
export class UnprocessableEntityError extends APIError<422, Headers> {
|
|
125
|
+
export class UnprocessableEntityError extends APIError<422, Headers> {
|
|
126
|
+
}
|
|
127
127
|
|
|
128
|
-
export class RateLimitError extends APIError<429, Headers> {
|
|
128
|
+
export class RateLimitError extends APIError<429, Headers> {
|
|
129
|
+
}
|
|
129
130
|
|
|
130
|
-
export class InternalServerError extends APIError<number, Headers> {
|
|
131
|
+
export class InternalServerError extends APIError<number, Headers> {
|
|
132
|
+
}
|
package/src/error.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** @deprecated Import from ./core/error instead */
|
|
2
|
-
export * from
|
|
2
|
+
export * from "./core/error"
|
package/src/index.ts
CHANGED
|
@@ -5,18 +5,4 @@ export { Postpeer as default } from './client';
|
|
|
5
5
|
export { type Uploadable, toFile } from './core/uploads';
|
|
6
6
|
export { APIPromise } from './core/api-promise';
|
|
7
7
|
export { Postpeer, type ClientOptions } from './client';
|
|
8
|
-
export {
|
|
9
|
-
PostpeerError,
|
|
10
|
-
APIError,
|
|
11
|
-
APIConnectionError,
|
|
12
|
-
APIConnectionTimeoutError,
|
|
13
|
-
APIUserAbortError,
|
|
14
|
-
NotFoundError,
|
|
15
|
-
ConflictError,
|
|
16
|
-
RateLimitError,
|
|
17
|
-
BadRequestError,
|
|
18
|
-
AuthenticationError,
|
|
19
|
-
InternalServerError,
|
|
20
|
-
PermissionDeniedError,
|
|
21
|
-
UnprocessableEntityError,
|
|
22
|
-
} from './core/error';
|
|
8
|
+
export { PostpeerError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError } from './core/error';
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
-
export type Fetch = (
|
|
3
|
+
export type Fetch = (
|
|
4
|
+
input: string | URL | Request,
|
|
5
|
+
init?: RequestInit,
|
|
6
|
+
) => Promise<Response>
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* An alias to the builtin `RequestInit` type so we can
|
|
7
10
|
* easily alias it in import statements if there are name clashes.
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
*
|
|
12
|
+
* https://developer.mozilla.org/docs/Web/API/RequestInit
|
|
10
13
|
*/
|
|
11
14
|
type _RequestInit = RequestInit;
|
|
12
15
|
|
|
13
16
|
/**
|
|
14
17
|
* An alias to the builtin `Response` type so we can
|
|
15
18
|
* easily alias it in import statements if there are name clashes.
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
*
|
|
20
|
+
* https://developer.mozilla.org/docs/Web/API/Response
|
|
18
21
|
*/
|
|
19
22
|
type _Response = Response;
|
|
20
23
|
|
|
@@ -51,15 +54,7 @@ type _Array<T> = Array<T>;
|
|
|
51
54
|
*/
|
|
52
55
|
type _Record<K extends keyof any, T> = Record<K, T>;
|
|
53
56
|
|
|
54
|
-
export type {
|
|
55
|
-
_Array as Array,
|
|
56
|
-
_BodyInit as BodyInit,
|
|
57
|
-
_HeadersInit as HeadersInit,
|
|
58
|
-
_Record as Record,
|
|
59
|
-
_RequestInfo as RequestInfo,
|
|
60
|
-
_RequestInit as RequestInit,
|
|
61
|
-
_Response as Response,
|
|
62
|
-
};
|
|
57
|
+
export type { _Array as Array, _BodyInit as BodyInit, _HeadersInit as HeadersInit, _Record as Record, _RequestInfo as RequestInfo, _RequestInit as RequestInit, _Response as Response };
|
|
63
58
|
|
|
64
59
|
/**
|
|
65
60
|
* A copy of the builtin `EndingType` type as it isn't fully supported in certain
|
|
@@ -25,11 +25,7 @@ function getDetectedPlatform(): DetectedPlatform {
|
|
|
25
25
|
if (typeof EdgeRuntime !== 'undefined') {
|
|
26
26
|
return 'edge';
|
|
27
27
|
}
|
|
28
|
-
if (
|
|
29
|
-
Object.prototype.toString.call(
|
|
30
|
-
typeof (globalThis as any).process !== 'undefined' ? (globalThis as any).process : 0,
|
|
31
|
-
) === '[object process]'
|
|
32
|
-
) {
|
|
28
|
+
if (Object.prototype.toString.call(typeof (globalThis as any).process !== 'undefined' ? (globalThis as any).process : 0) === '[object process]') {
|
|
33
29
|
return 'node';
|
|
34
30
|
}
|
|
35
31
|
return 'unknown';
|
package/src/internal/errors.ts
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
export function isAbortError(err: unknown) {
|
|
4
4
|
return (
|
|
5
|
-
typeof err === 'object' &&
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
(('name' in err && (err as any).name === 'AbortError') ||
|
|
5
|
+
typeof err === 'object' && err !== null && (
|
|
6
|
+
// Spec-compliant fetch implementations
|
|
7
|
+
('name' in err && (err as any).name === 'AbortError') ||
|
|
9
8
|
// Expo fetch
|
|
10
|
-
('message' in err && String((err as any).message).includes('FetchRequestCanceledException'))
|
|
9
|
+
('message' in err && String((err as any).message).includes('FetchRequestCanceledException'))
|
|
10
|
+
)
|
|
11
11
|
);
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -24,10 +24,10 @@ export const castToError = (err: any): Error => {
|
|
|
24
24
|
if (err.name) error.name = err.name;
|
|
25
25
|
return error;
|
|
26
26
|
}
|
|
27
|
-
} catch {}
|
|
27
|
+
} catch { }
|
|
28
28
|
try {
|
|
29
29
|
return new Error(JSON.stringify(err));
|
|
30
|
-
} catch {}
|
|
30
|
+
} catch { }
|
|
31
31
|
}
|
|
32
32
|
return new Error(err);
|
|
33
33
|
};
|
package/src/internal/headers.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
-
import { isReadonlyArray } from
|
|
3
|
+
import { isReadonlyArray } from "./utils/values";
|
|
4
4
|
|
|
5
5
|
type HeaderValue = string | undefined | null;
|
|
6
6
|
export type HeadersLike =
|
|
@@ -51,7 +51,7 @@ function* iterateHeaders(headers: HeadersLike): IterableIterator<readonly [strin
|
|
|
51
51
|
}
|
|
52
52
|
for (let row of iter) {
|
|
53
53
|
const name = row[0];
|
|
54
|
-
if (typeof name !==
|
|
54
|
+
if (typeof name !== "string") throw new TypeError("expected header name to be a string")
|
|
55
55
|
const values = isReadonlyArray(row[1]) ? row[1] : [row[1]];
|
|
56
56
|
let didClear = false;
|
|
57
57
|
for (const value of values) {
|
package/src/internal/parse.ts
CHANGED
|
@@ -16,6 +16,7 @@ export type APIResponseProps = {
|
|
|
16
16
|
export async function defaultParseResponse<T>(client: Postpeer, props: APIResponseProps): Promise<T> {
|
|
17
17
|
const { response, requestLogID, retryOfRequestLogID, startTime } = props;
|
|
18
18
|
const body = await (async () => {
|
|
19
|
+
|
|
19
20
|
// fetch refuses to read the body when the status code is 204.
|
|
20
21
|
if (response.status === 204) {
|
|
21
22
|
return null as T;
|
|
@@ -42,15 +43,6 @@ export async function defaultParseResponse<T>(client: Postpeer, props: APIRespon
|
|
|
42
43
|
const text = await response.text();
|
|
43
44
|
return text as unknown as T;
|
|
44
45
|
})();
|
|
45
|
-
loggerFor(client).debug(
|
|
46
|
-
`[${requestLogID}] response parsed`,
|
|
47
|
-
formatRequestDetails({
|
|
48
|
-
retryOfRequestLogID,
|
|
49
|
-
url: response.url,
|
|
50
|
-
status: response.status,
|
|
51
|
-
body,
|
|
52
|
-
durationMs: Date.now() - startTime,
|
|
53
|
-
}),
|
|
54
|
-
);
|
|
46
|
+
loggerFor(client).debug(`[${requestLogID}] response parsed`, formatRequestDetails({ retryOfRequestLogID, url: response.url, status: response.status, body, durationMs: Date.now() - startTime }));
|
|
55
47
|
return body;
|
|
56
48
|
}
|