spitch 1.26.1 → 1.48.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +62 -2
  2. package/README.md +8 -8
  3. package/client.d.mts +6 -5
  4. package/client.d.mts.map +1 -1
  5. package/client.d.ts +6 -5
  6. package/client.d.ts.map +1 -1
  7. package/client.js +28 -21
  8. package/client.js.map +1 -1
  9. package/client.mjs +28 -21
  10. package/client.mjs.map +1 -1
  11. package/internal/utils/env.js +2 -2
  12. package/internal/utils/env.js.map +1 -1
  13. package/internal/utils/env.mjs +2 -2
  14. package/internal/utils/env.mjs.map +1 -1
  15. package/internal/utils/query.d.mts +5 -0
  16. package/internal/utils/query.d.mts.map +1 -0
  17. package/internal/utils/query.d.ts +5 -0
  18. package/internal/utils/query.d.ts.map +1 -0
  19. package/internal/utils/query.js +23 -0
  20. package/internal/utils/query.js.map +1 -0
  21. package/internal/utils/query.mjs +20 -0
  22. package/internal/utils/query.mjs.map +1 -0
  23. package/internal/utils.d.mts +1 -0
  24. package/internal/utils.d.ts +1 -0
  25. package/internal/utils.js +1 -0
  26. package/internal/utils.js.map +1 -1
  27. package/internal/utils.mjs +1 -0
  28. package/package.json +12 -1
  29. package/resources/files.d.mts +6 -13
  30. package/resources/files.d.mts.map +1 -1
  31. package/resources/files.d.ts +6 -13
  32. package/resources/files.d.ts.map +1 -1
  33. package/resources/files.js.map +1 -1
  34. package/resources/files.mjs.map +1 -1
  35. package/resources/index.d.mts +1 -1
  36. package/resources/index.d.mts.map +1 -1
  37. package/resources/index.d.ts +1 -1
  38. package/resources/index.d.ts.map +1 -1
  39. package/src/client.ts +42 -33
  40. package/src/internal/utils/env.ts +2 -2
  41. package/src/internal/utils/query.ts +23 -0
  42. package/src/internal/utils.ts +1 -0
  43. package/src/resources/files.ts +8 -18
  44. package/src/resources/index.ts +2 -2
  45. package/src/version.ts +1 -1
  46. package/version.d.mts +1 -1
  47. package/version.d.ts +1 -1
  48. package/version.js +1 -1
  49. package/version.mjs +1 -1
package/src/client.ts CHANGED
@@ -11,6 +11,7 @@ import type { APIResponseProps } from './internal/parse';
11
11
  import { getPlatformHeaders } from './internal/detect-platform';
12
12
  import * as Shims from './internal/shims';
13
13
  import * as Opts from './internal/request-options';
14
+ import { stringifyQuery } from './internal/utils/query';
14
15
  import { VERSION } from './version';
15
16
  import * as Errors from './core/error';
16
17
  import * as Pagination from './core/pagination';
@@ -19,15 +20,15 @@ import * as Uploads from './core/uploads';
19
20
  import * as API from './resources/index';
20
21
  import { APIPromise } from './core/api-promise';
21
22
  import {
22
- File,
23
23
  FileDeleteResponse,
24
24
  FileDownloadParams,
25
25
  FileDownloadResponse,
26
26
  FileListParams,
27
+ FileMeta,
28
+ FileMetasFilesCursor,
27
29
  FileUploadParams,
28
30
  FileUsage,
29
31
  Files,
30
- FilesFilesCursor,
31
32
  } from './resources/files';
32
33
  import {
33
34
  Segment,
@@ -241,21 +242,8 @@ export class Spitch {
241
242
  /**
242
243
  * Basic re-implementation of `qs.stringify` for primitive types.
243
244
  */
244
- protected stringifyQuery(query: Record<string, unknown>): string {
245
- return Object.entries(query)
246
- .filter(([_, value]) => typeof value !== 'undefined')
247
- .map(([key, value]) => {
248
- if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
249
- return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
250
- }
251
- if (value === null) {
252
- return `${encodeURIComponent(key)}=`;
253
- }
254
- throw new Errors.SpitchError(
255
- `Cannot stringify type ${typeof value}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`,
256
- );
257
- })
258
- .join('&');
245
+ protected stringifyQuery(query: object | Record<string, unknown>): string {
246
+ return stringifyQuery(query);
259
247
  }
260
248
 
261
249
  private getUserAgent(): string {
@@ -287,12 +275,13 @@ export class Spitch {
287
275
  : new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
288
276
 
289
277
  const defaultQuery = this.defaultQuery();
290
- if (!isEmptyObj(defaultQuery)) {
291
- query = { ...defaultQuery, ...query };
278
+ const pathQuery = Object.fromEntries(url.searchParams);
279
+ if (!isEmptyObj(defaultQuery) || !isEmptyObj(pathQuery)) {
280
+ query = { ...pathQuery, ...defaultQuery, ...query };
292
281
  }
293
282
 
294
283
  if (typeof query === 'object' && query && !Array.isArray(query)) {
295
- url.search = this.stringifyQuery(query as Record<string, unknown>);
284
+ url.search = this.stringifyQuery(query);
296
285
  }
297
286
 
298
287
  return url.toString();
@@ -476,7 +465,7 @@ export class Spitch {
476
465
  loggerFor(this).info(`${responseInfo} - ${retryMessage}`);
477
466
 
478
467
  const errText = await response.text().catch((err: any) => castToError(err).message);
479
- const errJSON = safeJSON(errText);
468
+ const errJSON = safeJSON(errText) as any;
480
469
  const errMessage = errJSON ? undefined : errText;
481
470
 
482
471
  loggerFor(this).debug(
@@ -513,9 +502,14 @@ export class Spitch {
513
502
  getAPIList<Item, PageClass extends Pagination.AbstractPage<Item> = Pagination.AbstractPage<Item>>(
514
503
  path: string,
515
504
  Page: new (...args: any[]) => PageClass,
516
- opts?: RequestOptions,
505
+ opts?: PromiseOrValue<RequestOptions>,
517
506
  ): Pagination.PagePromise<PageClass, Item> {
518
- return this.requestAPIList(Page, { method: 'get', path, ...opts });
507
+ return this.requestAPIList(
508
+ Page,
509
+ opts && 'then' in opts ?
510
+ opts.then((opts) => ({ method: 'get', path, ...opts }))
511
+ : { method: 'get', path, ...opts },
512
+ );
519
513
  }
520
514
 
521
515
  requestAPIList<
@@ -523,7 +517,7 @@ export class Spitch {
523
517
  PageClass extends Pagination.AbstractPage<Item> = Pagination.AbstractPage<Item>,
524
518
  >(
525
519
  Page: new (...args: ConstructorParameters<typeof Pagination.AbstractPage>) => PageClass,
526
- options: FinalRequestOptions,
520
+ options: PromiseOrValue<FinalRequestOptions>,
527
521
  ): Pagination.PagePromise<PageClass, Item> {
528
522
  const request = this.makeRequest(options, null, undefined);
529
523
  return new Pagination.PagePromise<PageClass, Item>(this as any as Spitch, request, Page);
@@ -536,7 +530,7 @@ export class Spitch {
536
530
  controller: AbortController,
537
531
  ): Promise<Response> {
538
532
  const { signal, method, ...options } = init || {};
539
- const abort = controller.abort.bind(controller);
533
+ const abort = this._makeAbort(controller);
540
534
  if (signal) signal.addEventListener('abort', abort, { once: true });
541
535
 
542
536
  const timeout = setTimeout(abort, ms);
@@ -616,9 +610,9 @@ export class Spitch {
616
610
  }
617
611
  }
618
612
 
619
- // If the API asks us to wait a certain amount of time (and it's a reasonable amount),
620
- // just do what it says, but otherwise calculate a default
621
- if (!(timeoutMillis && 0 <= timeoutMillis && timeoutMillis < 60 * 1000)) {
613
+ // If the API asks us to wait a certain amount of time, just do what it
614
+ // says, but otherwise calculate a default
615
+ if (timeoutMillis === undefined) {
622
616
  const maxRetries = options.maxRetries ?? this.maxRetries;
623
617
  timeoutMillis = this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries);
624
618
  }
@@ -628,8 +622,8 @@ export class Spitch {
628
622
  }
629
623
 
630
624
  private calculateDefaultRetryTimeoutMillis(retriesRemaining: number, maxRetries: number): number {
631
- const initialRetryDelay = 0.5;
632
- const maxRetryDelay = 8.0;
625
+ const initialRetryDelay = 0.35;
626
+ const maxRetryDelay = 10.0;
633
627
 
634
628
  const numRetries = maxRetries - retriesRemaining;
635
629
 
@@ -707,6 +701,12 @@ export class Spitch {
707
701
  return headers.values;
708
702
  }
709
703
 
704
+ private _makeAbort(controller: AbortController) {
705
+ // note: we can't just inline this method inside `fetchWithTimeout()` because then the closure
706
+ // would capture all request options, and cause a memory leak.
707
+ return () => controller.abort();
708
+ }
709
+
710
710
  private buildBody({ options: { body, headers: rawHeaders } }: { options: FinalRequestOptions }): {
711
711
  bodyHeaders: HeadersLike;
712
712
  body: BodyInit | undefined;
@@ -739,6 +739,14 @@ export class Spitch {
739
739
  (Symbol.iterator in body && 'next' in body && typeof body.next === 'function'))
740
740
  ) {
741
741
  return { bodyHeaders: undefined, body: Shims.ReadableStreamFrom(body as AsyncIterable<Uint8Array>) };
742
+ } else if (
743
+ typeof body === 'object' &&
744
+ headers.values.get('content-type') === 'application/x-www-form-urlencoded'
745
+ ) {
746
+ return {
747
+ bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
748
+ body: this.stringifyQuery(body),
749
+ };
742
750
  } else {
743
751
  return this.#encoder({ body, headers });
744
752
  }
@@ -773,6 +781,7 @@ export class Spitch {
773
781
 
774
782
  Spitch.Speech = Speech;
775
783
  Spitch.Text = Text;
784
+ Spitch.Files = Files;
776
785
 
777
786
  export declare namespace Spitch {
778
787
  export type RequestOptions = Opts.RequestOptions;
@@ -797,12 +806,12 @@ export declare namespace Spitch {
797
806
  };
798
807
 
799
808
  export {
800
- type Files as Files,
801
- type File as File,
809
+ Files as Files,
810
+ type FileMeta as FileMeta,
802
811
  type FileUsage as FileUsage,
803
812
  type FileDeleteResponse as FileDeleteResponse,
804
813
  type FileDownloadResponse as FileDownloadResponse,
805
- type FilesFilesCursor as FilesFilesCursor,
814
+ type FileMetasFilesCursor as FileMetasFilesCursor,
806
815
  type FileListParams as FileListParams,
807
816
  type FileDownloadParams as FileDownloadParams,
808
817
  type FileUploadParams as FileUploadParams,
@@ -9,10 +9,10 @@
9
9
  */
10
10
  export const readEnv = (env: string): string | undefined => {
11
11
  if (typeof (globalThis as any).process !== 'undefined') {
12
- return (globalThis as any).process.env?.[env]?.trim() ?? undefined;
12
+ return (globalThis as any).process.env?.[env]?.trim() || undefined;
13
13
  }
14
14
  if (typeof (globalThis as any).Deno !== 'undefined') {
15
- return (globalThis as any).Deno.env?.get?.(env)?.trim();
15
+ return (globalThis as any).Deno.env?.get?.(env)?.trim() || undefined;
16
16
  }
17
17
  return undefined;
18
18
  };
@@ -0,0 +1,23 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { SpitchError } from '../../core/error';
4
+
5
+ /**
6
+ * Basic re-implementation of `qs.stringify` for primitive types.
7
+ */
8
+ export function stringifyQuery(query: object | Record<string, unknown>) {
9
+ return Object.entries(query)
10
+ .filter(([_, value]) => typeof value !== 'undefined')
11
+ .map(([key, value]) => {
12
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
13
+ return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
14
+ }
15
+ if (value === null) {
16
+ return `${encodeURIComponent(key)}=`;
17
+ }
18
+ throw new SpitchError(
19
+ `Cannot stringify type ${typeof value}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`,
20
+ );
21
+ })
22
+ .join('&');
23
+ }
@@ -6,3 +6,4 @@ export * from './utils/env';
6
6
  export * from './utils/log';
7
7
  export * from './utils/uuid';
8
8
  export * from './utils/sleep';
9
+ export * from './utils/query';
@@ -15,8 +15,8 @@ export class Files extends APIResource {
15
15
  list(
16
16
  query: FileListParams | null | undefined = {},
17
17
  options?: RequestOptions,
18
- ): PagePromise<FilesFilesCursor, File> {
19
- return this._client.getAPIList('/v1/files', FilesCursor<File>, { query, ...options });
18
+ ): PagePromise<FileMetasFilesCursor, FileMeta> {
19
+ return this._client.getAPIList('/v1/files', FilesCursor<FileMeta>, { query, ...options });
20
20
  }
21
21
 
22
22
  /**
@@ -40,14 +40,14 @@ export class Files extends APIResource {
40
40
  /**
41
41
  * Get File
42
42
  */
43
- get(fileID: string, options?: RequestOptions): APIPromise<File> {
43
+ get(fileID: string, options?: RequestOptions): APIPromise<FileMeta> {
44
44
  return this._client.get(path`/v1/files/${fileID}`, options);
45
45
  }
46
46
 
47
47
  /**
48
48
  * Upload a file to your storage.
49
49
  */
50
- upload(body: FileUploadParams, options?: RequestOptions): APIPromise<File> {
50
+ upload(body: FileUploadParams, options?: RequestOptions): APIPromise<FileMeta> {
51
51
  return this._client.post('/v1/files', multipartFormRequestOptions({ body, ...options }, this._client));
52
52
  }
53
53
 
@@ -59,12 +59,12 @@ export class Files extends APIResource {
59
59
  }
60
60
  }
61
61
 
62
- export type FilesFilesCursor = FilesCursor<File>;
62
+ export type FileMetasFilesCursor = FilesCursor<FileMeta>;
63
63
 
64
64
  /**
65
65
  * Metadata info for this file.
66
66
  */
67
- export interface File {
67
+ export interface FileMeta {
68
68
  created_at: string;
69
69
 
70
70
  file_id: string;
@@ -105,15 +105,6 @@ export interface FileUsage {
105
105
  used_bytes?: number;
106
106
  }
107
107
 
108
- /**
109
- * an array of file information.
110
- */
111
- export interface Files {
112
- items: Array<File>;
113
-
114
- next_cursor?: string | null;
115
- }
116
-
117
108
  export interface FileDeleteResponse {
118
109
  status?: boolean;
119
110
  }
@@ -132,12 +123,11 @@ export interface FileUploadParams {
132
123
 
133
124
  export declare namespace Files {
134
125
  export {
135
- type File as File,
126
+ type FileMeta as FileMeta,
136
127
  type FileUsage as FileUsage,
137
- type Files as Files,
138
128
  type FileDeleteResponse as FileDeleteResponse,
139
129
  type FileDownloadResponse as FileDownloadResponse,
140
- type FilesFilesCursor as FilesFilesCursor,
130
+ type FileMetasFilesCursor as FileMetasFilesCursor,
141
131
  type FileListParams as FileListParams,
142
132
  type FileDownloadParams as FileDownloadParams,
143
133
  type FileUploadParams as FileUploadParams,
@@ -2,14 +2,14 @@
2
2
 
3
3
  export {
4
4
  Files,
5
- type File,
5
+ type FileMeta,
6
6
  type FileUsage,
7
7
  type FileDeleteResponse,
8
8
  type FileDownloadResponse,
9
9
  type FileListParams,
10
10
  type FileDownloadParams,
11
11
  type FileUploadParams,
12
- type FilesFilesCursor,
12
+ type FileMetasFilesCursor,
13
13
  } from './files';
14
14
  export {
15
15
  Speech,
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '1.26.1'; // x-release-please-version
1
+ export const VERSION = '1.48.0'; // x-release-please-version
package/version.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "1.26.1";
1
+ export declare const VERSION = "1.48.0";
2
2
  //# sourceMappingURL=version.d.mts.map
package/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "1.26.1";
1
+ export declare const VERSION = "1.48.0";
2
2
  //# sourceMappingURL=version.d.ts.map
package/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '1.26.1'; // x-release-please-version
4
+ exports.VERSION = '1.48.0'; // x-release-please-version
5
5
  //# sourceMappingURL=version.js.map
package/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '1.26.1'; // x-release-please-version
1
+ export const VERSION = '1.48.0'; // x-release-please-version
2
2
  //# sourceMappingURL=version.mjs.map