spitch 0.0.1-alpha.1 → 1.16.1
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 +4 -5
- package/LICENSE +1 -1
- package/README.md +61 -27
- package/_shims/index.d.ts +2 -0
- package/_shims/index.js +5 -1
- package/_shims/index.mjs +5 -1
- package/_shims/web-runtime.js +1 -1
- package/_shims/web-runtime.js.map +1 -1
- package/_shims/web-runtime.mjs +1 -1
- package/_shims/web-runtime.mjs.map +1 -1
- package/core.d.ts +22 -9
- package/core.d.ts.map +1 -1
- package/core.js +50 -29
- package/core.js.map +1 -1
- package/core.mjs +51 -30
- package/core.mjs.map +1 -1
- package/error.d.ts +18 -24
- package/error.d.ts.map +1 -1
- package/error.js +1 -31
- package/error.js.map +1 -1
- package/error.mjs +1 -31
- package/error.mjs.map +1 -1
- package/index.d.mts +15 -18
- package/index.d.ts +15 -18
- package/index.d.ts.map +1 -1
- package/index.js +29 -11
- package/index.js.map +1 -1
- package/index.mjs +13 -10
- package/index.mjs.map +1 -1
- package/package.json +4 -4
- package/resource.d.ts +1 -1
- package/resource.d.ts.map +1 -1
- package/resource.js.map +1 -1
- package/resource.mjs.map +1 -1
- package/resources/index.d.ts +2 -2
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +2 -2
- package/resources/index.mjs.map +1 -1
- package/resources/speech.d.ts +24 -13
- package/resources/speech.d.ts.map +1 -1
- package/resources/speech.js +8 -4
- package/resources/speech.js.map +1 -1
- package/resources/speech.mjs +8 -4
- package/resources/speech.mjs.map +1 -1
- package/resources/text.d.ts +20 -6
- package/resources/text.d.ts.map +1 -1
- package/resources/text.js +6 -2
- package/resources/text.js.map +1 -1
- package/resources/text.mjs +6 -2
- package/resources/text.mjs.map +1 -1
- package/resources.d.ts +2 -0
- package/resources.d.ts.map +1 -0
- package/resources.js +18 -0
- package/resources.js.map +1 -0
- package/resources.mjs +2 -0
- package/resources.mjs.map +1 -0
- package/src/_shims/index.d.ts +2 -0
- package/src/_shims/index.js +5 -1
- package/src/_shims/index.mjs +5 -1
- package/src/_shims/web-runtime.ts +1 -1
- package/src/core.ts +81 -37
- package/src/error.ts +25 -41
- package/src/index.ts +52 -25
- package/src/resource.ts +1 -1
- package/src/resources/index.ts +13 -2
- package/src/resources/speech.ts +69 -15
- package/src/resources/text.ts +38 -7
- package/src/resources.ts +1 -0
- package/src/version.ts +1 -1
- package/version.d.ts +1 -1
- package/version.d.ts.map +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/resources/speech.js
CHANGED
|
@@ -33,16 +33,20 @@ class Speech extends resource_1.APIResource {
|
|
|
33
33
|
*/
|
|
34
34
|
generate(params, options) {
|
|
35
35
|
const { stream, ...body } = params;
|
|
36
|
-
return this._client.post('/v1/speech', {
|
|
36
|
+
return this._client.post('/v1/speech', {
|
|
37
|
+
query: { stream },
|
|
38
|
+
body,
|
|
39
|
+
...options,
|
|
40
|
+
headers: { Accept: 'audio/wav', ...options?.headers },
|
|
41
|
+
__binaryResponse: true,
|
|
42
|
+
});
|
|
37
43
|
}
|
|
38
44
|
/**
|
|
39
45
|
* Transcribe
|
|
40
46
|
*/
|
|
41
|
-
|
|
47
|
+
transcribe(body, options) {
|
|
42
48
|
return this._client.post('/v1/transcriptions', Core.multipartFormRequestOptions({ body, ...options }));
|
|
43
49
|
}
|
|
44
50
|
}
|
|
45
51
|
exports.Speech = Speech;
|
|
46
|
-
(function (Speech) {
|
|
47
|
-
})(Speech = exports.Speech || (exports.Speech = {}));
|
|
48
52
|
//# sourceMappingURL=speech.js.map
|
package/resources/speech.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"speech.js","sourceRoot":"","sources":["../src/resources/speech.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;;;;;;;;;;;;;;;;;;;;;;;;AAEtF,6CAA0C;AAC1C,iDAAgC;
|
|
1
|
+
{"version":3,"file":"speech.js","sourceRoot":"","sources":["../src/resources/speech.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;;;;;;;;;;;;;;;;;;;;;;;;AAEtF,6CAA0C;AAC1C,iDAAgC;AAGhC,MAAa,MAAO,SAAQ,sBAAW;IACrC;;OAEG;IACH,QAAQ,CAAC,MAA4B,EAAE,OAA6B;QAClE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;QACnC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE;YACrC,KAAK,EAAE,EAAE,MAAM,EAAE;YACjB,IAAI;YACJ,GAAG,OAAO;YACV,OAAO,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE;YACrD,gBAAgB,EAAE,IAAI;SACvB,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,UAAU,CACR,IAA4B,EAC5B,OAA6B;QAE7B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,oBAAoB,EAAE,IAAI,CAAC,2BAA2B,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC,CAAC;IACzG,CAAC;CACF;AAxBD,wBAwBC"}
|
package/resources/speech.mjs
CHANGED
|
@@ -7,15 +7,19 @@ export class Speech extends APIResource {
|
|
|
7
7
|
*/
|
|
8
8
|
generate(params, options) {
|
|
9
9
|
const { stream, ...body } = params;
|
|
10
|
-
return this._client.post('/v1/speech', {
|
|
10
|
+
return this._client.post('/v1/speech', {
|
|
11
|
+
query: { stream },
|
|
12
|
+
body,
|
|
13
|
+
...options,
|
|
14
|
+
headers: { Accept: 'audio/wav', ...options?.headers },
|
|
15
|
+
__binaryResponse: true,
|
|
16
|
+
});
|
|
11
17
|
}
|
|
12
18
|
/**
|
|
13
19
|
* Transcribe
|
|
14
20
|
*/
|
|
15
|
-
|
|
21
|
+
transcribe(body, options) {
|
|
16
22
|
return this._client.post('/v1/transcriptions', Core.multipartFormRequestOptions({ body, ...options }));
|
|
17
23
|
}
|
|
18
24
|
}
|
|
19
|
-
(function (Speech) {
|
|
20
|
-
})(Speech || (Speech = {}));
|
|
21
25
|
//# sourceMappingURL=speech.mjs.map
|
package/resources/speech.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"speech.mjs","sourceRoot":"","sources":["../src/resources/speech.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;OACf,KAAK,IAAI;
|
|
1
|
+
{"version":3,"file":"speech.mjs","sourceRoot":"","sources":["../src/resources/speech.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;OACf,KAAK,IAAI;AAGhB,MAAM,OAAO,MAAO,SAAQ,WAAW;IACrC;;OAEG;IACH,QAAQ,CAAC,MAA4B,EAAE,OAA6B;QAClE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;QACnC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE;YACrC,KAAK,EAAE,EAAE,MAAM,EAAE;YACjB,IAAI;YACJ,GAAG,OAAO;YACV,OAAO,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE;YACrD,gBAAgB,EAAE,IAAI;SACvB,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,UAAU,CACR,IAA4B,EAC5B,OAA6B;QAE7B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,oBAAoB,EAAE,IAAI,CAAC,2BAA2B,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC,CAAC;IACzG,CAAC;CACF"}
|
package/resources/text.d.ts
CHANGED
|
@@ -1,19 +1,33 @@
|
|
|
1
1
|
import { APIResource } from "../resource.js";
|
|
2
2
|
import * as Core from "../core.js";
|
|
3
|
-
import * as TextAPI from "./text.js";
|
|
4
3
|
export declare class Text extends APIResource {
|
|
5
4
|
/**
|
|
6
5
|
* Tone Mark
|
|
7
6
|
*/
|
|
8
|
-
toneMark(body: TextToneMarkParams, options?: Core.RequestOptions): Core.APIPromise<
|
|
7
|
+
toneMark(body: TextToneMarkParams, options?: Core.RequestOptions): Core.APIPromise<TextToneMarkResponse>;
|
|
8
|
+
/**
|
|
9
|
+
* Translate
|
|
10
|
+
*/
|
|
11
|
+
translate(body: TextTranslateParams, options?: Core.RequestOptions): Core.APIPromise<TextTranslateResponse>;
|
|
12
|
+
}
|
|
13
|
+
export interface TextToneMarkResponse {
|
|
14
|
+
request_id: string;
|
|
15
|
+
text?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface TextTranslateResponse {
|
|
18
|
+
request_id: string;
|
|
19
|
+
text?: string;
|
|
9
20
|
}
|
|
10
|
-
export type TextToneMarkResponse = unknown;
|
|
11
21
|
export interface TextToneMarkParams {
|
|
12
|
-
language: 'yo' | 'en' | 'ha' | 'ig';
|
|
22
|
+
language: 'yo' | 'en' | 'ha' | 'ig' | 'am';
|
|
23
|
+
text: string;
|
|
24
|
+
}
|
|
25
|
+
export interface TextTranslateParams {
|
|
26
|
+
source: 'yo' | 'en' | 'ha' | 'ig' | 'am';
|
|
27
|
+
target: 'yo' | 'en' | 'ha' | 'ig' | 'am';
|
|
13
28
|
text: string;
|
|
14
29
|
}
|
|
15
30
|
export declare namespace Text {
|
|
16
|
-
export
|
|
17
|
-
export import TextToneMarkParams = TextAPI.TextToneMarkParams;
|
|
31
|
+
export { type TextToneMarkResponse as TextToneMarkResponse, type TextTranslateResponse as TextTranslateResponse, type TextToneMarkParams as TextToneMarkParams, type TextTranslateParams as TextTranslateParams, };
|
|
18
32
|
}
|
|
19
33
|
//# sourceMappingURL=text.d.ts.map
|
package/resources/text.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../src/resources/text.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,IAAI,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../src/resources/text.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,IAAI,MAAM,SAAS,CAAC;AAEhC,qBAAa,IAAK,SAAQ,WAAW;IACnC;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,kBAAkB,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,UAAU,CAAC,oBAAoB,CAAC;IAIxG;;OAEG;IACH,SAAS,CACP,IAAI,EAAE,mBAAmB,EACzB,OAAO,CAAC,EAAE,IAAI,CAAC,cAAc,GAC5B,IAAI,CAAC,UAAU,CAAC,qBAAqB,CAAC;CAG1C;AAED,MAAM,WAAW,oBAAoB;IACnC,UAAU,EAAE,MAAM,CAAC;IAEnB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,qBAAqB;IACpC,UAAU,EAAE,MAAM,CAAC;IAEnB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;IAE3C,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;IAEzC,MAAM,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;IAEzC,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,CAAC,OAAO,WAAW,IAAI,CAAC;IAC5B,OAAO,EACL,KAAK,oBAAoB,IAAI,oBAAoB,EACjD,KAAK,qBAAqB,IAAI,qBAAqB,EACnD,KAAK,kBAAkB,IAAI,kBAAkB,EAC7C,KAAK,mBAAmB,IAAI,mBAAmB,GAChD,CAAC;CACH"}
|
package/resources/text.js
CHANGED
|
@@ -10,8 +10,12 @@ class Text extends resource_1.APIResource {
|
|
|
10
10
|
toneMark(body, options) {
|
|
11
11
|
return this._client.post('/v1/diacritics', { body, ...options });
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Translate
|
|
15
|
+
*/
|
|
16
|
+
translate(body, options) {
|
|
17
|
+
return this._client.post('/v1/translate', { body, ...options });
|
|
18
|
+
}
|
|
13
19
|
}
|
|
14
20
|
exports.Text = Text;
|
|
15
|
-
(function (Text) {
|
|
16
|
-
})(Text = exports.Text || (exports.Text = {}));
|
|
17
21
|
//# sourceMappingURL=text.js.map
|
package/resources/text.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","sourceRoot":"","sources":["../src/resources/text.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,6CAA0C;
|
|
1
|
+
{"version":3,"file":"text.js","sourceRoot":"","sources":["../src/resources/text.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,6CAA0C;AAG1C,MAAa,IAAK,SAAQ,sBAAW;IACnC;;OAEG;IACH,QAAQ,CAAC,IAAwB,EAAE,OAA6B;QAC9D,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IACnE,CAAC;IAED;;OAEG;IACH,SAAS,CACP,IAAyB,EACzB,OAA6B;QAE7B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,eAAe,EAAE,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IAClE,CAAC;CACF;AAjBD,oBAiBC"}
|
package/resources/text.mjs
CHANGED
|
@@ -7,7 +7,11 @@ export class Text extends APIResource {
|
|
|
7
7
|
toneMark(body, options) {
|
|
8
8
|
return this._client.post('/v1/diacritics', { body, ...options });
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Translate
|
|
12
|
+
*/
|
|
13
|
+
translate(body, options) {
|
|
14
|
+
return this._client.post('/v1/translate', { body, ...options });
|
|
15
|
+
}
|
|
10
16
|
}
|
|
11
|
-
(function (Text) {
|
|
12
|
-
})(Text || (Text = {}));
|
|
13
17
|
//# sourceMappingURL=text.mjs.map
|
package/resources/text.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.mjs","sourceRoot":"","sources":["../src/resources/text.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;
|
|
1
|
+
{"version":3,"file":"text.mjs","sourceRoot":"","sources":["../src/resources/text.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;AAGtB,MAAM,OAAO,IAAK,SAAQ,WAAW;IACnC;;OAEG;IACH,QAAQ,CAAC,IAAwB,EAAE,OAA6B;QAC9D,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IACnE,CAAC;IAED;;OAEG;IACH,SAAS,CACP,IAAyB,EACzB,OAA6B;QAE7B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,eAAe,EAAE,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IAClE,CAAC;CACF"}
|
package/resources.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resources.d.ts","sourceRoot":"","sources":["src/resources.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC"}
|
package/resources.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./resources/index.js"), exports);
|
|
18
|
+
//# sourceMappingURL=resources.js.map
|
package/resources.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resources.js","sourceRoot":"","sources":["src/resources.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAkC"}
|
package/resources.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resources.mjs","sourceRoot":"","sources":["src/resources.ts"],"names":[],"mappings":""}
|
package/src/_shims/index.d.ts
CHANGED
|
@@ -79,3 +79,5 @@ export function fileFromPath(path: string, options?: FileFromPathOptions): Promi
|
|
|
79
79
|
export function fileFromPath(path: string, filename?: string, options?: FileFromPathOptions): Promise<File>;
|
|
80
80
|
|
|
81
81
|
export function isFsReadStream(value: any): value is FsReadStream;
|
|
82
|
+
|
|
83
|
+
export const init: () => void;
|
package/src/_shims/index.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
*/
|
|
4
4
|
const shims = require('./registry');
|
|
5
5
|
const auto = require('spitch/_shims/auto/runtime');
|
|
6
|
-
|
|
6
|
+
exports.init = () => {
|
|
7
|
+
if (!shims.kind) shims.setShims(auto.getRuntime(), { auto: true });
|
|
8
|
+
};
|
|
7
9
|
for (const property of Object.keys(shims)) {
|
|
8
10
|
Object.defineProperty(exports, property, {
|
|
9
11
|
get() {
|
|
@@ -11,3 +13,5 @@ for (const property of Object.keys(shims)) {
|
|
|
11
13
|
},
|
|
12
14
|
});
|
|
13
15
|
}
|
|
16
|
+
|
|
17
|
+
exports.init();
|
package/src/_shims/index.mjs
CHANGED
|
@@ -3,5 +3,9 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import * as shims from './registry.mjs';
|
|
5
5
|
import * as auto from "./auto/runtime";
|
|
6
|
-
|
|
6
|
+
export const init = () => {
|
|
7
|
+
if (!shims.kind) shims.setShims(auto.getRuntime(), { auto: true });
|
|
8
|
+
};
|
|
7
9
|
export * from './registry.mjs';
|
|
10
|
+
|
|
11
|
+
init();
|
|
@@ -95,7 +95,7 @@ export function getRuntime({ manuallyImported }: { manuallyImported?: boolean }
|
|
|
95
95
|
getDefaultAgent: (url: string) => undefined,
|
|
96
96
|
fileFromPath: () => {
|
|
97
97
|
throw new Error(
|
|
98
|
-
'The `fileFromPath` function is only supported in Node. See the README for more details: https://www.github.com/spi-tch/
|
|
98
|
+
'The `fileFromPath` function is only supported in Node. See the README for more details: https://www.github.com/spi-tch/node#file-uploads',
|
|
99
99
|
);
|
|
100
100
|
},
|
|
101
101
|
isFsReadStream: (value: any) => false,
|
package/src/core.ts
CHANGED
|
@@ -16,7 +16,12 @@ import {
|
|
|
16
16
|
type RequestInit,
|
|
17
17
|
type Response,
|
|
18
18
|
type HeadersInit,
|
|
19
|
+
init,
|
|
19
20
|
} from './_shims/index';
|
|
21
|
+
|
|
22
|
+
// try running side effects outside of _shims/index to workaround https://github.com/vercel/next.js/issues/76881
|
|
23
|
+
init();
|
|
24
|
+
|
|
20
25
|
export { type Response };
|
|
21
26
|
import { BlobLike, isBlobLike, isMultipartBody } from './uploads';
|
|
22
27
|
export {
|
|
@@ -28,6 +33,20 @@ export {
|
|
|
28
33
|
|
|
29
34
|
export type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
30
35
|
|
|
36
|
+
/**
|
|
37
|
+
* An alias to the builtin `Array` type so we can
|
|
38
|
+
* easily alias it in import statements if there are name clashes.
|
|
39
|
+
*/
|
|
40
|
+
type _Array<T> = Array<T>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* An alias to the builtin `Record` type so we can
|
|
44
|
+
* easily alias it in import statements if there are name clashes.
|
|
45
|
+
*/
|
|
46
|
+
type _Record<K extends keyof any, T> = Record<K, T>;
|
|
47
|
+
|
|
48
|
+
export type { _Array as Array, _Record as Record };
|
|
49
|
+
|
|
31
50
|
type PromiseOrValue<T> = T | Promise<T>;
|
|
32
51
|
|
|
33
52
|
type APIResponseProps = {
|
|
@@ -48,8 +67,8 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
|
|
|
48
67
|
}
|
|
49
68
|
|
|
50
69
|
const contentType = response.headers.get('content-type');
|
|
51
|
-
const
|
|
52
|
-
|
|
70
|
+
const mediaType = contentType?.split(';')[0]?.trim();
|
|
71
|
+
const isJSON = mediaType?.includes('application/json') || mediaType?.endsWith('+json');
|
|
53
72
|
if (isJSON) {
|
|
54
73
|
const json = await response.json();
|
|
55
74
|
|
|
@@ -84,8 +103,10 @@ export class APIPromise<T> extends Promise<T> {
|
|
|
84
103
|
});
|
|
85
104
|
}
|
|
86
105
|
|
|
87
|
-
_thenUnwrap<U>(transform: (data: T) => U): APIPromise<U> {
|
|
88
|
-
return new APIPromise(this.responsePromise, async (props) =>
|
|
106
|
+
_thenUnwrap<U>(transform: (data: T, props: APIResponseProps) => U): APIPromise<U> {
|
|
107
|
+
return new APIPromise(this.responsePromise, async (props) =>
|
|
108
|
+
transform(await this.parseResponse(props), props),
|
|
109
|
+
);
|
|
89
110
|
}
|
|
90
111
|
|
|
91
112
|
/**
|
|
@@ -149,6 +170,7 @@ export class APIPromise<T> extends Promise<T> {
|
|
|
149
170
|
|
|
150
171
|
export abstract class APIClient {
|
|
151
172
|
baseURL: string;
|
|
173
|
+
#baseURLOverridden: boolean;
|
|
152
174
|
maxRetries: number;
|
|
153
175
|
timeout: number;
|
|
154
176
|
httpAgent: Agent | undefined;
|
|
@@ -158,23 +180,26 @@ export abstract class APIClient {
|
|
|
158
180
|
|
|
159
181
|
constructor({
|
|
160
182
|
baseURL,
|
|
183
|
+
baseURLOverridden,
|
|
161
184
|
maxRetries = 2,
|
|
162
185
|
timeout = 60000, // 1 minute
|
|
163
186
|
httpAgent,
|
|
164
|
-
fetch:
|
|
187
|
+
fetch: overriddenFetch,
|
|
165
188
|
}: {
|
|
166
189
|
baseURL: string;
|
|
190
|
+
baseURLOverridden: boolean;
|
|
167
191
|
maxRetries?: number | undefined;
|
|
168
192
|
timeout: number | undefined;
|
|
169
193
|
httpAgent: Agent | undefined;
|
|
170
194
|
fetch: Fetch | undefined;
|
|
171
195
|
}) {
|
|
172
196
|
this.baseURL = baseURL;
|
|
197
|
+
this.#baseURLOverridden = baseURLOverridden;
|
|
173
198
|
this.maxRetries = validatePositiveInteger('maxRetries', maxRetries);
|
|
174
199
|
this.timeout = validatePositiveInteger('timeout', timeout);
|
|
175
200
|
this.httpAgent = httpAgent;
|
|
176
201
|
|
|
177
|
-
this.fetch =
|
|
202
|
+
this.fetch = overriddenFetch ?? fetch;
|
|
178
203
|
}
|
|
179
204
|
|
|
180
205
|
protected authHeaders(opts: FinalRequestOptions): Headers {
|
|
@@ -275,10 +300,11 @@ export abstract class APIClient {
|
|
|
275
300
|
}
|
|
276
301
|
|
|
277
302
|
buildRequest<Req>(
|
|
278
|
-
|
|
303
|
+
inputOptions: FinalRequestOptions<Req>,
|
|
279
304
|
{ retryCount = 0 }: { retryCount?: number } = {},
|
|
280
305
|
): { req: RequestInit; url: string; timeout: number } {
|
|
281
|
-
const
|
|
306
|
+
const options = { ...inputOptions };
|
|
307
|
+
const { method, path, query, defaultBaseURL, headers: headers = {} } = options;
|
|
282
308
|
|
|
283
309
|
const body =
|
|
284
310
|
ArrayBuffer.isView(options.body) || (options.__binaryRequest && typeof options.body === 'string') ?
|
|
@@ -288,11 +314,11 @@ export abstract class APIClient {
|
|
|
288
314
|
: null;
|
|
289
315
|
const contentLength = this.calculateContentLength(body);
|
|
290
316
|
|
|
291
|
-
const url = this.buildURL(path!, query);
|
|
317
|
+
const url = this.buildURL(path!, query, defaultBaseURL);
|
|
292
318
|
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
|
|
293
|
-
|
|
319
|
+
options.timeout = options.timeout ?? this.timeout;
|
|
294
320
|
const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
|
|
295
|
-
const minAgentTimeout = timeout + 1000;
|
|
321
|
+
const minAgentTimeout = options.timeout + 1000;
|
|
296
322
|
if (
|
|
297
323
|
typeof (httpAgent as any)?.options?.timeout === 'number' &&
|
|
298
324
|
minAgentTimeout > ((httpAgent as any).options.timeout ?? 0)
|
|
@@ -305,8 +331,8 @@ export abstract class APIClient {
|
|
|
305
331
|
}
|
|
306
332
|
|
|
307
333
|
if (this.idempotencyHeader && method !== 'get') {
|
|
308
|
-
if (!
|
|
309
|
-
headers[this.idempotencyHeader] =
|
|
334
|
+
if (!inputOptions.idempotencyKey) inputOptions.idempotencyKey = this.defaultIdempotencyKey();
|
|
335
|
+
headers[this.idempotencyHeader] = inputOptions.idempotencyKey;
|
|
310
336
|
}
|
|
311
337
|
|
|
312
338
|
const reqHeaders = this.buildHeaders({ options, headers, contentLength, retryCount });
|
|
@@ -321,7 +347,7 @@ export abstract class APIClient {
|
|
|
321
347
|
signal: options.signal ?? null,
|
|
322
348
|
};
|
|
323
349
|
|
|
324
|
-
return { req, url, timeout };
|
|
350
|
+
return { req, url, timeout: options.timeout };
|
|
325
351
|
}
|
|
326
352
|
|
|
327
353
|
private buildHeaders({
|
|
@@ -349,11 +375,22 @@ export abstract class APIClient {
|
|
|
349
375
|
delete reqHeaders['content-type'];
|
|
350
376
|
}
|
|
351
377
|
|
|
352
|
-
// Don't set
|
|
353
|
-
// which can contain nulls, instead of `reqHeaders` to account
|
|
354
|
-
|
|
378
|
+
// Don't set theses headers if they were already set or removed through default headers or by the caller.
|
|
379
|
+
// We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to account
|
|
380
|
+
// for the removal case.
|
|
381
|
+
if (
|
|
382
|
+
getHeader(defaultHeaders, 'x-stainless-retry-count') === undefined &&
|
|
383
|
+
getHeader(headers, 'x-stainless-retry-count') === undefined
|
|
384
|
+
) {
|
|
355
385
|
reqHeaders['x-stainless-retry-count'] = String(retryCount);
|
|
356
386
|
}
|
|
387
|
+
if (
|
|
388
|
+
getHeader(defaultHeaders, 'x-stainless-timeout') === undefined &&
|
|
389
|
+
getHeader(headers, 'x-stainless-timeout') === undefined &&
|
|
390
|
+
options.timeout
|
|
391
|
+
) {
|
|
392
|
+
reqHeaders['x-stainless-timeout'] = String(Math.trunc(options.timeout / 1000));
|
|
393
|
+
}
|
|
357
394
|
|
|
358
395
|
this.validateHeaders(reqHeaders, headers);
|
|
359
396
|
|
|
@@ -381,7 +418,7 @@ export abstract class APIClient {
|
|
|
381
418
|
!headers ? {}
|
|
382
419
|
: Symbol.iterator in headers ?
|
|
383
420
|
Object.fromEntries(Array.from(headers as Iterable<string[]>).map((header) => [...header]))
|
|
384
|
-
: { ...headers }
|
|
421
|
+
: { ...(headers as any as Record<string, string>) }
|
|
385
422
|
);
|
|
386
423
|
}
|
|
387
424
|
|
|
@@ -390,7 +427,7 @@ export abstract class APIClient {
|
|
|
390
427
|
error: Object | undefined,
|
|
391
428
|
message: string | undefined,
|
|
392
429
|
headers: Headers | undefined,
|
|
393
|
-
) {
|
|
430
|
+
): APIError {
|
|
394
431
|
return APIError.generate(status, error, message, headers);
|
|
395
432
|
}
|
|
396
433
|
|
|
@@ -470,11 +507,12 @@ export abstract class APIClient {
|
|
|
470
507
|
return new PagePromise<PageClass, Item>(this, request, Page);
|
|
471
508
|
}
|
|
472
509
|
|
|
473
|
-
buildURL<Req>(path: string, query: Req | null | undefined): string {
|
|
510
|
+
buildURL<Req>(path: string, query: Req | null | undefined, defaultBaseURL?: string | undefined): string {
|
|
511
|
+
const baseURL = (!this.#baseURLOverridden && defaultBaseURL) || this.baseURL;
|
|
474
512
|
const url =
|
|
475
513
|
isAbsoluteURL(path) ?
|
|
476
514
|
new URL(path)
|
|
477
|
-
: new URL(
|
|
515
|
+
: new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
|
|
478
516
|
|
|
479
517
|
const defaultQuery = this.defaultQuery();
|
|
480
518
|
if (!isEmptyObj(defaultQuery)) {
|
|
@@ -516,20 +554,24 @@ export abstract class APIClient {
|
|
|
516
554
|
|
|
517
555
|
const timeout = setTimeout(() => controller.abort(), ms);
|
|
518
556
|
|
|
557
|
+
const fetchOptions = {
|
|
558
|
+
signal: controller.signal as any,
|
|
559
|
+
...options,
|
|
560
|
+
};
|
|
561
|
+
if (fetchOptions.method) {
|
|
562
|
+
// Custom methods like 'patch' need to be uppercased
|
|
563
|
+
// See https://github.com/nodejs/undici/issues/2294
|
|
564
|
+
fetchOptions.method = fetchOptions.method.toUpperCase();
|
|
565
|
+
}
|
|
566
|
+
|
|
519
567
|
return (
|
|
520
|
-
this
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
clearTimeout(timeout);
|
|
525
|
-
})
|
|
568
|
+
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
|
|
569
|
+
this.fetch.call(undefined, url, fetchOptions).finally(() => {
|
|
570
|
+
clearTimeout(timeout);
|
|
571
|
+
})
|
|
526
572
|
);
|
|
527
573
|
}
|
|
528
574
|
|
|
529
|
-
protected getRequestClient(): RequestClient {
|
|
530
|
-
return { fetch: this.fetch };
|
|
531
|
-
}
|
|
532
|
-
|
|
533
575
|
private shouldRetry(response: Response): boolean {
|
|
534
576
|
// Note this is not a standard header.
|
|
535
577
|
const shouldRetryHeader = response.headers.get('x-should-retry');
|
|
@@ -662,9 +704,9 @@ export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
|
|
|
662
704
|
return await this.#client.requestAPIList(this.constructor as any, nextOptions);
|
|
663
705
|
}
|
|
664
706
|
|
|
665
|
-
async *iterPages() {
|
|
707
|
+
async *iterPages(): AsyncGenerator<this> {
|
|
666
708
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
667
|
-
let page:
|
|
709
|
+
let page: this = this;
|
|
668
710
|
yield page;
|
|
669
711
|
while (page.hasNextPage()) {
|
|
670
712
|
page = await page.getNextPage();
|
|
@@ -672,7 +714,7 @@ export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
|
|
|
672
714
|
}
|
|
673
715
|
}
|
|
674
716
|
|
|
675
|
-
async *[Symbol.asyncIterator]() {
|
|
717
|
+
async *[Symbol.asyncIterator](): AsyncGenerator<Item> {
|
|
676
718
|
for await (const page of this.iterPages()) {
|
|
677
719
|
for (const item of page.getPaginatedItems()) {
|
|
678
720
|
yield item;
|
|
@@ -715,7 +757,7 @@ export class PagePromise<
|
|
|
715
757
|
* console.log(item)
|
|
716
758
|
* }
|
|
717
759
|
*/
|
|
718
|
-
async *[Symbol.asyncIterator]() {
|
|
760
|
+
async *[Symbol.asyncIterator](): AsyncGenerator<Item> {
|
|
719
761
|
const page = await this;
|
|
720
762
|
for await (const item of page) {
|
|
721
763
|
yield item;
|
|
@@ -755,6 +797,7 @@ export type RequestOptions<
|
|
|
755
797
|
query?: Req | undefined;
|
|
756
798
|
body?: Req | null | undefined;
|
|
757
799
|
headers?: Headers | undefined;
|
|
800
|
+
defaultBaseURL?: string | undefined;
|
|
758
801
|
|
|
759
802
|
maxRetries?: number;
|
|
760
803
|
stream?: boolean | undefined;
|
|
@@ -776,6 +819,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
|
|
|
776
819
|
query: true,
|
|
777
820
|
body: true,
|
|
778
821
|
headers: true,
|
|
822
|
+
defaultBaseURL: true,
|
|
779
823
|
|
|
780
824
|
maxRetries: true,
|
|
781
825
|
stream: true,
|
|
@@ -970,8 +1014,8 @@ export const safeJSON = (text: string) => {
|
|
|
970
1014
|
}
|
|
971
1015
|
};
|
|
972
1016
|
|
|
973
|
-
// https://
|
|
974
|
-
const startsWithSchemeRegexp =
|
|
1017
|
+
// https://url.spec.whatwg.org/#url-scheme-string
|
|
1018
|
+
const startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;
|
|
975
1019
|
const isAbsoluteURL = (url: string): boolean => {
|
|
976
1020
|
return startsWithSchemeRegexp.test(url);
|
|
977
1021
|
};
|
package/src/error.ts
CHANGED
|
@@ -4,17 +4,19 @@ import { castToError, Headers } from './core';
|
|
|
4
4
|
|
|
5
5
|
export class SpitchError extends Error {}
|
|
6
6
|
|
|
7
|
-
export class APIError
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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 SpitchError {
|
|
12
|
+
/** HTTP status for the response that caused the error */
|
|
13
|
+
readonly status: TStatus;
|
|
14
|
+
/** HTTP headers for the response that caused the error */
|
|
15
|
+
readonly headers: THeaders;
|
|
16
|
+
/** JSON body of the response that caused the error */
|
|
17
|
+
readonly error: TError;
|
|
18
|
+
|
|
19
|
+
constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders) {
|
|
18
20
|
super(`${APIError.makeMessage(status, error, message)}`);
|
|
19
21
|
this.status = status;
|
|
20
22
|
this.headers = headers;
|
|
@@ -47,8 +49,8 @@ export class APIError extends SpitchError {
|
|
|
47
49
|
errorResponse: Object | undefined,
|
|
48
50
|
message: string | undefined,
|
|
49
51
|
headers: Headers | undefined,
|
|
50
|
-
) {
|
|
51
|
-
if (!status) {
|
|
52
|
+
): APIError {
|
|
53
|
+
if (!status || !headers) {
|
|
52
54
|
return new APIConnectionError({ message, cause: castToError(errorResponse) });
|
|
53
55
|
}
|
|
54
56
|
|
|
@@ -90,17 +92,13 @@ export class APIError extends SpitchError {
|
|
|
90
92
|
}
|
|
91
93
|
}
|
|
92
94
|
|
|
93
|
-
export class APIUserAbortError extends APIError {
|
|
94
|
-
override readonly status: undefined = undefined;
|
|
95
|
-
|
|
95
|
+
export class APIUserAbortError extends APIError<undefined, undefined, undefined> {
|
|
96
96
|
constructor({ message }: { message?: string } = {}) {
|
|
97
97
|
super(undefined, undefined, message || 'Request was aborted.', undefined);
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
export class APIConnectionError extends APIError {
|
|
102
|
-
override readonly status: undefined = undefined;
|
|
103
|
-
|
|
101
|
+
export class APIConnectionError extends APIError<undefined, undefined, undefined> {
|
|
104
102
|
constructor({ message, cause }: { message?: string | undefined; cause?: Error | undefined }) {
|
|
105
103
|
super(undefined, undefined, message || 'Connection error.', undefined);
|
|
106
104
|
// in some environments the 'cause' property is already declared
|
|
@@ -115,32 +113,18 @@ export class APIConnectionTimeoutError extends APIConnectionError {
|
|
|
115
113
|
}
|
|
116
114
|
}
|
|
117
115
|
|
|
118
|
-
export class BadRequestError extends APIError {
|
|
119
|
-
override readonly status: 400 = 400;
|
|
120
|
-
}
|
|
116
|
+
export class BadRequestError extends APIError<400, Headers> {}
|
|
121
117
|
|
|
122
|
-
export class AuthenticationError extends APIError {
|
|
123
|
-
override readonly status: 401 = 401;
|
|
124
|
-
}
|
|
118
|
+
export class AuthenticationError extends APIError<401, Headers> {}
|
|
125
119
|
|
|
126
|
-
export class PermissionDeniedError extends APIError {
|
|
127
|
-
override readonly status: 403 = 403;
|
|
128
|
-
}
|
|
120
|
+
export class PermissionDeniedError extends APIError<403, Headers> {}
|
|
129
121
|
|
|
130
|
-
export class NotFoundError extends APIError {
|
|
131
|
-
override readonly status: 404 = 404;
|
|
132
|
-
}
|
|
122
|
+
export class NotFoundError extends APIError<404, Headers> {}
|
|
133
123
|
|
|
134
|
-
export class ConflictError extends APIError {
|
|
135
|
-
override readonly status: 409 = 409;
|
|
136
|
-
}
|
|
124
|
+
export class ConflictError extends APIError<409, Headers> {}
|
|
137
125
|
|
|
138
|
-
export class UnprocessableEntityError extends APIError {
|
|
139
|
-
override readonly status: 422 = 422;
|
|
140
|
-
}
|
|
126
|
+
export class UnprocessableEntityError extends APIError<422, Headers> {}
|
|
141
127
|
|
|
142
|
-
export class RateLimitError extends APIError {
|
|
143
|
-
override readonly status: 429 = 429;
|
|
144
|
-
}
|
|
128
|
+
export class RateLimitError extends APIError<429, Headers> {}
|
|
145
129
|
|
|
146
|
-
export class InternalServerError extends APIError {}
|
|
130
|
+
export class InternalServerError extends APIError<number, Headers> {}
|