magic-hour 0.43.0 → 0.44.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/README.md +82 -77
- package/dist/core/api-error.d.ts +6 -0
- package/dist/core/api-error.js +11 -0
- package/dist/core/api-promise.d.ts +28 -0
- package/dist/core/api-promise.js +252 -0
- package/dist/core/auth.d.ts +76 -0
- package/dist/core/auth.js +232 -0
- package/dist/core/binary-response.d.ts +6 -0
- package/dist/core/binary-response.js +10 -0
- package/dist/core/content-type.d.ts +4 -0
- package/dist/core/content-type.js +12 -0
- package/dist/core/core-client.d.ts +58 -0
- package/dist/core/core-client.js +168 -0
- package/dist/core/form-data.d.ts +26 -0
- package/dist/core/form-data.js +83 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.js +28 -0
- package/dist/core/logger.d.ts +48 -0
- package/dist/core/logger.js +82 -0
- package/dist/core/query.d.ts +8 -0
- package/dist/core/query.js +77 -0
- package/dist/core/runtime.d.ts +4 -0
- package/dist/core/runtime.js +20 -0
- package/dist/core/zod.d.ts +13 -0
- package/dist/core/zod.js +60 -0
- package/dist/environment.d.ts +1 -1
- package/dist/environment.js +1 -1
- package/dist/examples/logging-example.d.ts +10 -0
- package/dist/examples/logging-example.js +137 -0
- package/dist/logging-client.d.ts +39 -0
- package/dist/logging-client.js +116 -0
- package/dist/resources/v1/ai-voice-cloner/index.d.ts +2 -0
- package/dist/resources/v1/ai-voice-cloner/index.js +5 -0
- package/dist/resources/v1/ai-voice-cloner/request-types.d.ts +31 -0
- package/dist/resources/v1/ai-voice-cloner/request-types.js +77 -0
- package/dist/resources/v1/ai-voice-cloner/resource-client.d.ts +55 -0
- package/dist/resources/v1/ai-voice-cloner/resource-client.js +99 -0
- package/dist/resources/v1/index.d.ts +1 -0
- package/dist/resources/v1/index.js +2 -1
- package/dist/resources/v1/resource-client.d.ts +3 -0
- package/dist/resources/v1/resource-client.js +6 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/v1-ai-voice-cloner-create-body-assets.d.ts +27 -0
- package/dist/types/v1-ai-voice-cloner-create-body-assets.js +67 -0
- package/dist/types/v1-ai-voice-cloner-create-body-style.d.ts +22 -0
- package/dist/types/v1-ai-voice-cloner-create-body-style.js +67 -0
- package/dist/types/v1-ai-voice-cloner-create-body.d.ts +31 -0
- package/dist/types/v1-ai-voice-cloner-create-body.js +77 -0
- package/dist/types/v1-ai-voice-cloner-create-response.d.ts +29 -0
- package/dist/types/v1-ai-voice-cloner-create-response.js +71 -0
- package/package.json +1 -1
|
@@ -0,0 +1,232 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
45
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.OAuth2 = exports.AuthKey = exports.AuthBearer = exports.AuthBasic = void 0;
|
|
49
|
+
const qs_1 = __importDefault(require("qs"));
|
|
50
|
+
const form_urlencoded_1 = __importDefault(require("form-urlencoded"));
|
|
51
|
+
const js_base64_1 = require("js-base64");
|
|
52
|
+
const jsonpointer = __importStar(require("jsonpointer"));
|
|
53
|
+
const runtime_1 = require("./runtime");
|
|
54
|
+
const api_error_1 = require("./api-error");
|
|
55
|
+
const AUTHORIZATION = "Authorization";
|
|
56
|
+
const COOKIE = "Cookie";
|
|
57
|
+
class AuthBasic {
|
|
58
|
+
constructor(username, password) {
|
|
59
|
+
this.username = username !== null && username !== void 0 ? username : null;
|
|
60
|
+
this.password = password !== null && password !== void 0 ? password : null;
|
|
61
|
+
}
|
|
62
|
+
applyAuth(cfg) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
var _a;
|
|
65
|
+
const headers = (_a = cfg.headers) !== null && _a !== void 0 ? _a : {};
|
|
66
|
+
if (this.username !== null && this.password !== null) {
|
|
67
|
+
const encoded = (0, js_base64_1.toBase64)(`${this.username}:${this.password}`);
|
|
68
|
+
cfg.headers = headers;
|
|
69
|
+
headers[AUTHORIZATION] = `Basic ${encoded}`;
|
|
70
|
+
}
|
|
71
|
+
return cfg;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
setValue(val) {
|
|
75
|
+
this.username = val !== null && val !== void 0 ? val : null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.AuthBasic = AuthBasic;
|
|
79
|
+
class AuthBearer {
|
|
80
|
+
constructor(token) {
|
|
81
|
+
this.token = token !== null && token !== void 0 ? token : null;
|
|
82
|
+
}
|
|
83
|
+
applyAuth(cfg) {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
var _a;
|
|
86
|
+
const headers = (_a = cfg.headers) !== null && _a !== void 0 ? _a : {};
|
|
87
|
+
if (this.token !== null) {
|
|
88
|
+
cfg.headers = headers;
|
|
89
|
+
headers[AUTHORIZATION] = `Bearer ${this.token}`;
|
|
90
|
+
}
|
|
91
|
+
return cfg;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
setValue(val) {
|
|
95
|
+
this.token = val !== null && val !== void 0 ? val : null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.AuthBearer = AuthBearer;
|
|
99
|
+
class AuthKey {
|
|
100
|
+
constructor(name, location, key) {
|
|
101
|
+
this.name = name;
|
|
102
|
+
this.location = location;
|
|
103
|
+
this.key = key !== null && key !== void 0 ? key : null;
|
|
104
|
+
}
|
|
105
|
+
applyAuth(cfg) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
var _a, _b, _c, _d;
|
|
108
|
+
if (this.key === null) {
|
|
109
|
+
return cfg;
|
|
110
|
+
}
|
|
111
|
+
if (this.location === "query") {
|
|
112
|
+
const query = (_a = cfg.query) !== null && _a !== void 0 ? _a : [];
|
|
113
|
+
cfg.query = query;
|
|
114
|
+
query.push(qs_1.default.stringify({ [this.name]: this.key }));
|
|
115
|
+
}
|
|
116
|
+
else if (this.location === "header") {
|
|
117
|
+
const headers = (_b = cfg.headers) !== null && _b !== void 0 ? _b : {};
|
|
118
|
+
cfg.headers = headers;
|
|
119
|
+
headers[this.name] = this.key;
|
|
120
|
+
}
|
|
121
|
+
else if (this.location === "cookie" && runtime_1.RUNTIME.type === "browser") {
|
|
122
|
+
cfg.withCredentials = true;
|
|
123
|
+
}
|
|
124
|
+
else if (this.location === "cookie") {
|
|
125
|
+
const headers = (_c = cfg.headers) !== null && _c !== void 0 ? _c : {};
|
|
126
|
+
const cookies = (_d = headers[COOKIE]) !== null && _d !== void 0 ? _d : "";
|
|
127
|
+
headers[COOKIE] = `${cookies.length > 0 ? ";" : ""}${this.name}=${this.key}`;
|
|
128
|
+
cfg.headers = headers;
|
|
129
|
+
}
|
|
130
|
+
return cfg;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
setValue(val) {
|
|
134
|
+
this.key = val !== null && val !== void 0 ? val : null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.AuthKey = AuthKey;
|
|
138
|
+
function isOAuth2Password(val) {
|
|
139
|
+
return typeof val.username === "string" && typeof val.password === "string";
|
|
140
|
+
}
|
|
141
|
+
function isOAuth2ClientCredentials(val) {
|
|
142
|
+
return (typeof val.clientId === "string" && typeof val.clientSecret === "string");
|
|
143
|
+
}
|
|
144
|
+
class OAuth2 {
|
|
145
|
+
constructor(props) {
|
|
146
|
+
this.props = props;
|
|
147
|
+
}
|
|
148
|
+
refresh(form) {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
150
|
+
var _a, _b;
|
|
151
|
+
const { baseUrl, defaultTokenUrl, credentialsLocation, bodyContent, accessTokenPointer, expiresInPointer, } = this.props;
|
|
152
|
+
// build token url
|
|
153
|
+
let tokenUrl = (_a = form === null || form === void 0 ? void 0 : form.tokenUrl) !== null && _a !== void 0 ? _a : defaultTokenUrl;
|
|
154
|
+
if (tokenUrl.startsWith("/")) {
|
|
155
|
+
// relative token url
|
|
156
|
+
const base = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
157
|
+
tokenUrl = `${base}${tokenUrl}`;
|
|
158
|
+
}
|
|
159
|
+
let defaultGrantType = isOAuth2Password(form)
|
|
160
|
+
? "password"
|
|
161
|
+
: "client_credentials";
|
|
162
|
+
const reqHeaders = {};
|
|
163
|
+
const reqData = {
|
|
164
|
+
grant_type: (_b = form.grantType) !== null && _b !== void 0 ? _b : defaultGrantType,
|
|
165
|
+
};
|
|
166
|
+
// Add client credentials
|
|
167
|
+
if (isOAuth2ClientCredentials(form)) {
|
|
168
|
+
if (credentialsLocation === "basic_authorization_header") {
|
|
169
|
+
const encoded = (0, js_base64_1.toBase64)(`${form.clientId}:${form.clientSecret}`);
|
|
170
|
+
reqHeaders[AUTHORIZATION] = `Basic ${encoded}`;
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
reqData["client_id"] = form.clientId;
|
|
174
|
+
reqData["client_secret"] = form.clientSecret;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (isOAuth2Password(form)) {
|
|
178
|
+
reqData["username"] = form.username;
|
|
179
|
+
reqData["password"] = form.password;
|
|
180
|
+
}
|
|
181
|
+
if (typeof form.scope !== "undefined") {
|
|
182
|
+
reqData["scope"] = form.scope.join(" ");
|
|
183
|
+
}
|
|
184
|
+
const reqInit = { method: "POST" };
|
|
185
|
+
if (bodyContent === "form") {
|
|
186
|
+
reqInit.body = (0, form_urlencoded_1.default)(reqData);
|
|
187
|
+
reqHeaders["content-type"] = "application/x-www-form-urlencoded";
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
reqInit.body = JSON.stringify(reqData);
|
|
191
|
+
reqHeaders["content-type"] = "application/json";
|
|
192
|
+
}
|
|
193
|
+
reqInit.headers = reqHeaders;
|
|
194
|
+
const fetcherFn = runtime_1.RUNTIME.type === "node" || typeof fetch !== "function"
|
|
195
|
+
? require("node-fetch").default
|
|
196
|
+
: fetch;
|
|
197
|
+
const tokenRes = yield fetcherFn(tokenUrl, reqInit);
|
|
198
|
+
if (!tokenRes.ok) {
|
|
199
|
+
throw new api_error_1.ApiError({
|
|
200
|
+
method: "post",
|
|
201
|
+
path: tokenUrl,
|
|
202
|
+
headers: reqHeaders,
|
|
203
|
+
body: reqData,
|
|
204
|
+
contentType: reqHeaders["content-type"],
|
|
205
|
+
}, tokenRes);
|
|
206
|
+
}
|
|
207
|
+
const tokenResJson = yield tokenRes.json();
|
|
208
|
+
const accessTokenRaw = jsonpointer.get(tokenResJson, accessTokenPointer);
|
|
209
|
+
const accessToken = typeof accessTokenRaw === "string" ? accessTokenRaw : "";
|
|
210
|
+
const expiresInRaw = jsonpointer.get(tokenResJson, expiresInPointer);
|
|
211
|
+
const expiresInSecs = typeof expiresInRaw === "number" ? expiresInRaw : 600;
|
|
212
|
+
const now = new Date();
|
|
213
|
+
const expiresAt = new Date(now.getTime() + expiresInSecs * 1000);
|
|
214
|
+
return { accessToken, expiresAt };
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
applyAuth(cfg) {
|
|
218
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
219
|
+
if (this.props.form && !this.accessToken) {
|
|
220
|
+
const { accessToken, expiresAt } = yield this.refresh(this.props.form);
|
|
221
|
+
this.accessToken = accessToken;
|
|
222
|
+
this.expiresAt = expiresAt;
|
|
223
|
+
}
|
|
224
|
+
this.props.requestMutator.setValue(this.accessToken);
|
|
225
|
+
return yield this.props.requestMutator.applyAuth(cfg);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
setValue(val) {
|
|
229
|
+
throw "an OAuth2 auth provider can not a requestMutator";
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
exports.OAuth2 = OAuth2;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BinaryResponse = void 0;
|
|
4
|
+
class BinaryResponse {
|
|
5
|
+
constructor(content, contentType) {
|
|
6
|
+
this.content = content;
|
|
7
|
+
this.contentType = contentType;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.BinaryResponse = BinaryResponse;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.URL_FORM = exports.MULTIPART_FORM = exports.TEXT_PATTERN = exports.JSON_PATTERN = void 0;
|
|
4
|
+
// use a pattern to match json content type to handle
|
|
5
|
+
// base case of application/json as well as vendor specific
|
|
6
|
+
// headers e.g. application/vnd.github+json
|
|
7
|
+
exports.JSON_PATTERN = /^application\/(.+\+)?json/;
|
|
8
|
+
// use a pattern to match text types to handle base case
|
|
9
|
+
// of text/plain as well as more specific cases e.g. text/html
|
|
10
|
+
exports.TEXT_PATTERN = /^text\/(.+)/;
|
|
11
|
+
exports.MULTIPART_FORM = "multipart/form-data";
|
|
12
|
+
exports.URL_FORM = "application/x-www-form-urlencoded";
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type * as z from "zod";
|
|
2
|
+
import type { Response as NodeResponse } from "node-fetch";
|
|
3
|
+
import type { AuthProvider } from "./auth";
|
|
4
|
+
import { ApiPromise } from "./api-promise";
|
|
5
|
+
import { type QueryStyle } from "./query";
|
|
6
|
+
export interface CoreClientProps {
|
|
7
|
+
baseUrl: string | Record<string, string | undefined>;
|
|
8
|
+
timeout?: number | undefined;
|
|
9
|
+
auths?: Record<string, AuthProvider>;
|
|
10
|
+
}
|
|
11
|
+
export type ApiResponse = Response | NodeResponse;
|
|
12
|
+
export type HttpMethod = "get" | "post" | "put" | "patch" | "delete";
|
|
13
|
+
export type RequestConfig = {
|
|
14
|
+
method: HttpMethod;
|
|
15
|
+
path: string;
|
|
16
|
+
serviceName?: string | undefined;
|
|
17
|
+
responseSchema?: z.Schema;
|
|
18
|
+
responseStream?: boolean;
|
|
19
|
+
responseRaw?: boolean;
|
|
20
|
+
auth?: string[];
|
|
21
|
+
query?: string[];
|
|
22
|
+
body?: any;
|
|
23
|
+
bodyEncoding?: {
|
|
24
|
+
style?: Record<string, QueryStyle>;
|
|
25
|
+
explode?: Record<string, boolean>;
|
|
26
|
+
};
|
|
27
|
+
contentType?: string;
|
|
28
|
+
headers?: Record<string, string>;
|
|
29
|
+
opts?: RequestOptions | undefined;
|
|
30
|
+
withCredentials?: boolean;
|
|
31
|
+
};
|
|
32
|
+
export interface RequestOptions {
|
|
33
|
+
timeout?: number;
|
|
34
|
+
additionalHeaders?: Record<string, string>;
|
|
35
|
+
additionalQuery?: Record<string, string>;
|
|
36
|
+
}
|
|
37
|
+
export declare class CoreClient {
|
|
38
|
+
private baseUrl;
|
|
39
|
+
private auths;
|
|
40
|
+
private timeout;
|
|
41
|
+
constructor(props: CoreClientProps);
|
|
42
|
+
private applyAuths;
|
|
43
|
+
buildUrl(path: string, serviceName?: string): string;
|
|
44
|
+
private buildUrlFromCfg;
|
|
45
|
+
private applyHeaders;
|
|
46
|
+
private encodeBodyByContentType;
|
|
47
|
+
private buildRequestInit;
|
|
48
|
+
private request;
|
|
49
|
+
makeRequest<R>(cfg: RequestConfig): ApiPromise<R>;
|
|
50
|
+
}
|
|
51
|
+
export interface ResourceClientOptions {
|
|
52
|
+
lazyLoad?: boolean | undefined;
|
|
53
|
+
}
|
|
54
|
+
export declare class CoreResourceClient {
|
|
55
|
+
protected _client: CoreClient;
|
|
56
|
+
protected _opts: ResourceClientOptions;
|
|
57
|
+
constructor(client: CoreClient, opts: ResourceClientOptions);
|
|
58
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CoreResourceClient = exports.CoreClient = void 0;
|
|
13
|
+
const runtime_1 = require("./runtime");
|
|
14
|
+
const api_promise_1 = require("./api-promise");
|
|
15
|
+
const api_error_1 = require("./api-error");
|
|
16
|
+
const form_data_1 = require("./form-data");
|
|
17
|
+
const query_1 = require("./query");
|
|
18
|
+
const content_type_1 = require("./content-type");
|
|
19
|
+
const _DEFAULT_SERVICE_NAME = "__default_service__";
|
|
20
|
+
class CoreClient {
|
|
21
|
+
constructor(props) {
|
|
22
|
+
var _a;
|
|
23
|
+
this.baseUrl =
|
|
24
|
+
typeof props.baseUrl === "string"
|
|
25
|
+
? { [_DEFAULT_SERVICE_NAME]: props.baseUrl }
|
|
26
|
+
: props.baseUrl;
|
|
27
|
+
this.auths = (_a = props.auths) !== null && _a !== void 0 ? _a : {};
|
|
28
|
+
this.timeout = props.timeout;
|
|
29
|
+
}
|
|
30
|
+
applyAuths(cfg) {
|
|
31
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
var _a;
|
|
33
|
+
for (const name of (_a = cfg.auth) !== null && _a !== void 0 ? _a : []) {
|
|
34
|
+
const provider = this.auths[name];
|
|
35
|
+
if (provider) {
|
|
36
|
+
cfg = yield provider.applyAuth(cfg);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return cfg;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
buildUrl(path, serviceName = _DEFAULT_SERVICE_NAME) {
|
|
43
|
+
var _a;
|
|
44
|
+
const baseUrl = (_a = this.baseUrl[serviceName !== null && serviceName !== void 0 ? serviceName : _DEFAULT_SERVICE_NAME]) !== null && _a !== void 0 ? _a : "";
|
|
45
|
+
const base = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
46
|
+
const cleanPath = path.startsWith("/") ? path.slice(1) : path;
|
|
47
|
+
return `${base}/${cleanPath}`;
|
|
48
|
+
}
|
|
49
|
+
buildUrlFromCfg(cfg) {
|
|
50
|
+
var _a, _b, _c;
|
|
51
|
+
let url = this.buildUrl(cfg.path, cfg.serviceName);
|
|
52
|
+
const searchParams = new URLSearchParams(((_a = cfg.query) !== null && _a !== void 0 ? _a : []).join("&"));
|
|
53
|
+
for (const [key, val] of Object.entries((_c = (_b = cfg.opts) === null || _b === void 0 ? void 0 : _b.additionalQuery) !== null && _c !== void 0 ? _c : {})) {
|
|
54
|
+
searchParams.append(key, val);
|
|
55
|
+
}
|
|
56
|
+
if (searchParams.toString().length > 0) {
|
|
57
|
+
url += `?${searchParams.toString()}`;
|
|
58
|
+
}
|
|
59
|
+
return url;
|
|
60
|
+
}
|
|
61
|
+
applyHeaders(cfg, reqInit) {
|
|
62
|
+
var _a, _b, _c;
|
|
63
|
+
const finalHeaders = Object.assign(Object.assign(Object.assign(Object.assign({ "x-sideko-sdk-language": "Javascript", "x-sideko-runtime": runtime_1.RUNTIME.type }, ((_a = reqInit.headers) !== null && _a !== void 0 ? _a : {})), ((_b = cfg.headers) !== null && _b !== void 0 ? _b : {})), (cfg.contentType ? { "content-type": cfg.contentType } : {})), (_c = cfg.opts) === null || _c === void 0 ? void 0 : _c.additionalHeaders);
|
|
64
|
+
reqInit.headers = finalHeaders;
|
|
65
|
+
return reqInit;
|
|
66
|
+
}
|
|
67
|
+
encodeBodyByContentType(cfg, reqInit) {
|
|
68
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
69
|
+
const contentTypeOverride = (_c = (_b = (_a = cfg.opts) === null || _a === void 0 ? void 0 : _a.additionalHeaders) === null || _b === void 0 ? void 0 : _b["content-type"]) !== null && _c !== void 0 ? _c : (_e = (_d = cfg.opts) === null || _d === void 0 ? void 0 : _d.additionalHeaders) === null || _e === void 0 ? void 0 : _e["Content-Type"];
|
|
70
|
+
const contentType = (_f = contentTypeOverride !== null && contentTypeOverride !== void 0 ? contentTypeOverride : cfg.contentType) !== null && _f !== void 0 ? _f : "";
|
|
71
|
+
if (typeof cfg.body === "undefined") {
|
|
72
|
+
return reqInit;
|
|
73
|
+
}
|
|
74
|
+
else if (contentType.match(content_type_1.JSON_PATTERN)) {
|
|
75
|
+
reqInit.body = JSON.stringify(cfg.body);
|
|
76
|
+
}
|
|
77
|
+
else if (contentType.match(content_type_1.TEXT_PATTERN)) {
|
|
78
|
+
reqInit.body = String(cfg.body);
|
|
79
|
+
}
|
|
80
|
+
else if (contentType === content_type_1.MULTIPART_FORM) {
|
|
81
|
+
// encode form
|
|
82
|
+
const form = (0, form_data_1.createForm)(cfg.body);
|
|
83
|
+
const headers = (_g = reqInit.headers) !== null && _g !== void 0 ? _g : {};
|
|
84
|
+
if (runtime_1.RUNTIME.type === "node") {
|
|
85
|
+
// explicitly set boundary
|
|
86
|
+
headers["content-type"] = `${content_type_1.MULTIPART_FORM}; boundary=${form.getBoundary()}`;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
// the browser should automatically set the content type
|
|
90
|
+
delete headers["content-type"];
|
|
91
|
+
}
|
|
92
|
+
reqInit.headers = headers;
|
|
93
|
+
reqInit.body = form;
|
|
94
|
+
}
|
|
95
|
+
else if (contentType === content_type_1.URL_FORM) {
|
|
96
|
+
if (typeof cfg.body !== "object") {
|
|
97
|
+
throw new TypeError("x-www-form-urlencoded data must be an object at the top level");
|
|
98
|
+
}
|
|
99
|
+
// encode form data
|
|
100
|
+
const styleMap = (_j = (_h = cfg.bodyEncoding) === null || _h === void 0 ? void 0 : _h.style) !== null && _j !== void 0 ? _j : {};
|
|
101
|
+
const explodeMap = (_l = (_k = cfg.bodyEncoding) === null || _k === void 0 ? void 0 : _k.explode) !== null && _l !== void 0 ? _l : {};
|
|
102
|
+
const formData = Object.entries(cfg.body).map(([name, value]) => {
|
|
103
|
+
var _a, _b;
|
|
104
|
+
const style = (_a = styleMap[name]) !== null && _a !== void 0 ? _a : "form";
|
|
105
|
+
const explode = (_b = explodeMap[name]) !== null && _b !== void 0 ? _b : style === "form";
|
|
106
|
+
return (0, query_1.encodeQueryParam)({ name, value, style, explode });
|
|
107
|
+
});
|
|
108
|
+
reqInit.body = formData.join("&");
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
// we expect body to already be encoded in the correct fashion
|
|
112
|
+
reqInit.body = cfg.body;
|
|
113
|
+
}
|
|
114
|
+
return reqInit;
|
|
115
|
+
}
|
|
116
|
+
buildRequestInit(cfg) {
|
|
117
|
+
let reqInit = { method: cfg.method.toUpperCase() };
|
|
118
|
+
reqInit = this.applyHeaders(cfg, reqInit);
|
|
119
|
+
reqInit = this.encodeBodyByContentType(cfg, reqInit);
|
|
120
|
+
if (cfg.withCredentials) {
|
|
121
|
+
reqInit.credentials = "include";
|
|
122
|
+
}
|
|
123
|
+
return reqInit;
|
|
124
|
+
}
|
|
125
|
+
request(cfg) {
|
|
126
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
127
|
+
var _a, _b;
|
|
128
|
+
const fetcherFn = runtime_1.RUNTIME.type === "node" || typeof fetch !== "function"
|
|
129
|
+
? require("node-fetch").default
|
|
130
|
+
: fetch;
|
|
131
|
+
cfg = yield this.applyAuths(cfg);
|
|
132
|
+
const reqInit = this.buildRequestInit(cfg);
|
|
133
|
+
const url = this.buildUrlFromCfg(cfg);
|
|
134
|
+
const timeout = (_b = (_a = cfg.opts) === null || _a === void 0 ? void 0 : _a.timeout) !== null && _b !== void 0 ? _b : this.timeout;
|
|
135
|
+
const controller = new AbortController();
|
|
136
|
+
let timeoutId;
|
|
137
|
+
if (typeof timeout !== "undefined") {
|
|
138
|
+
timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
139
|
+
}
|
|
140
|
+
reqInit.signal = controller.signal;
|
|
141
|
+
const response = yield fetcherFn(url, reqInit);
|
|
142
|
+
if (timeoutId) {
|
|
143
|
+
clearTimeout(timeoutId);
|
|
144
|
+
}
|
|
145
|
+
if (!response.ok) {
|
|
146
|
+
throw new api_error_1.ApiError(cfg, response);
|
|
147
|
+
}
|
|
148
|
+
return response;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
makeRequest(cfg) {
|
|
152
|
+
var _a, _b;
|
|
153
|
+
return new api_promise_1.ApiPromise({
|
|
154
|
+
responsePromise: this.request(cfg),
|
|
155
|
+
responseRaw: (_a = cfg.responseRaw) !== null && _a !== void 0 ? _a : false,
|
|
156
|
+
responseStream: (_b = cfg.responseStream) !== null && _b !== void 0 ? _b : false,
|
|
157
|
+
responseSchema: cfg.responseSchema,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.CoreClient = CoreClient;
|
|
162
|
+
class CoreResourceClient {
|
|
163
|
+
constructor(client, opts) {
|
|
164
|
+
this._client = client;
|
|
165
|
+
this._opts = opts;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
exports.CoreResourceClient = CoreResourceClient;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ReadStream as NodeReadStream } from "node:fs";
|
|
2
|
+
export type UploadFile = FileLike | NodeReadStream;
|
|
3
|
+
export declare function isUploadFile(val: any): val is UploadFile;
|
|
4
|
+
export declare function isNodeReadStream(val: any): val is NodeReadStream;
|
|
5
|
+
/**
|
|
6
|
+
* Defines common File spec compatible with modern browser File, node-fetch, etc.
|
|
7
|
+
*/
|
|
8
|
+
export interface FileLike extends BlobLike {
|
|
9
|
+
lastModified: number;
|
|
10
|
+
name: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function isFileLike(val: any): val is FileLike;
|
|
13
|
+
/**
|
|
14
|
+
* Defines common Blob spec compatible with modern browser Blob, fetch-blob, etc.
|
|
15
|
+
*/
|
|
16
|
+
export interface BlobLike {
|
|
17
|
+
type: string;
|
|
18
|
+
size: number;
|
|
19
|
+
slice(start?: number, end?: number, type?: string): BlobLike;
|
|
20
|
+
text(): Promise<string>;
|
|
21
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
22
|
+
}
|
|
23
|
+
export declare function isBlobLike(val: any): val is BlobLike;
|
|
24
|
+
export declare function createForm(data: {
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}): any;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isUploadFile = isUploadFile;
|
|
4
|
+
exports.isNodeReadStream = isNodeReadStream;
|
|
5
|
+
exports.isFileLike = isFileLike;
|
|
6
|
+
exports.isBlobLike = isBlobLike;
|
|
7
|
+
exports.createForm = createForm;
|
|
8
|
+
const runtime_1 = require("./runtime");
|
|
9
|
+
function isUploadFile(val) {
|
|
10
|
+
return isFileLike(val) || isNodeReadStream(val);
|
|
11
|
+
}
|
|
12
|
+
function isNodeReadStream(val) {
|
|
13
|
+
// node read stream duck-typing check
|
|
14
|
+
return (runtime_1.RUNTIME.type === "node" &&
|
|
15
|
+
typeof val === "object" &&
|
|
16
|
+
typeof val.bytesRead === "number" &&
|
|
17
|
+
(typeof val.path === "string" || val.path instanceof Buffer) &&
|
|
18
|
+
typeof val.pending === "boolean" &&
|
|
19
|
+
typeof val.readable === "boolean" &&
|
|
20
|
+
typeof val.read === "function" &&
|
|
21
|
+
typeof val.setEncoding === "function" &&
|
|
22
|
+
typeof val.pause === "function" &&
|
|
23
|
+
typeof val.resume === "function" &&
|
|
24
|
+
typeof val.isPaused === "function" &&
|
|
25
|
+
typeof val.pipe === "function" &&
|
|
26
|
+
typeof val.unpipe === "function" &&
|
|
27
|
+
typeof val.unshift === "function" &&
|
|
28
|
+
typeof val.wrap === "function");
|
|
29
|
+
}
|
|
30
|
+
function isFileLike(val) {
|
|
31
|
+
// https://developer.mozilla.org/docs/Web/API/File
|
|
32
|
+
// https://github.com/node-fetch/fetch-blob/blob/main/file.js
|
|
33
|
+
return (typeof (val === null || val === void 0 ? void 0 : val.lastModified) === "number" &&
|
|
34
|
+
typeof (val === null || val === void 0 ? void 0 : val.name) === "string" &&
|
|
35
|
+
isBlobLike(val));
|
|
36
|
+
}
|
|
37
|
+
function isBlobLike(val) {
|
|
38
|
+
// https://developer.mozilla.org/docs/Web/API/Blob
|
|
39
|
+
// https://github.com/node-fetch/fetch-blob/blob/main/index.js
|
|
40
|
+
return (typeof (val === null || val === void 0 ? void 0 : val.size) === "number" &&
|
|
41
|
+
typeof (val === null || val === void 0 ? void 0 : val.type) === "string" &&
|
|
42
|
+
typeof (val === null || val === void 0 ? void 0 : val.slice) === "function" &&
|
|
43
|
+
typeof (val === null || val === void 0 ? void 0 : val.text) === "function" &&
|
|
44
|
+
typeof (val === null || val === void 0 ? void 0 : val.arrayBuffer) === "function");
|
|
45
|
+
}
|
|
46
|
+
function appendFormData(form, key, val) {
|
|
47
|
+
if (val === null || typeof val === "undefined") {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
else if (["string", "boolean", "number"].includes(typeof val)) {
|
|
51
|
+
form.append(key, val.toString());
|
|
52
|
+
}
|
|
53
|
+
else if (Array.isArray(val)) {
|
|
54
|
+
for (const arrVal of val) {
|
|
55
|
+
appendFormData(form, key, arrVal);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else if (isUploadFile(val)) {
|
|
59
|
+
form.append(key, val);
|
|
60
|
+
}
|
|
61
|
+
else if (typeof val === "object") {
|
|
62
|
+
for (const [objKey, objVal] of Object.entries(val)) {
|
|
63
|
+
appendFormData(form, `${key}[${objKey}]`, objVal);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
throw new TypeError(`Invalid value given to form: ${val}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function createForm(data) {
|
|
71
|
+
let form;
|
|
72
|
+
if (runtime_1.RUNTIME.type === "node") {
|
|
73
|
+
const NodeFormData = require("form-data");
|
|
74
|
+
form = new NodeFormData();
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
form = new FormData();
|
|
78
|
+
}
|
|
79
|
+
for (const [key, val] of Object.entries(data)) {
|
|
80
|
+
appendFormData(form, key, val);
|
|
81
|
+
}
|
|
82
|
+
return form;
|
|
83
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { ApiError } from "./api-error";
|
|
2
|
+
export { ApiPromise, EventSourceResponse } from "./api-promise";
|
|
3
|
+
export { AuthBasic, AuthBearer, AuthKey, AuthProvider, OAuth2, OAuth2ClientCredentials, OAuth2Password, } from "./auth";
|
|
4
|
+
export { BinaryResponse } from "./binary-response";
|
|
5
|
+
export { ApiResponse, CoreClient, CoreClientProps, ResourceClientOptions, CoreResourceClient, RequestOptions, } from "./core-client";
|
|
6
|
+
export { createForm, isUploadFile, UploadFile } from "./form-data";
|
|
7
|
+
export { encodeQueryParam } from "./query";
|
|
8
|
+
export { RUNTIME } from "./runtime";
|
|
9
|
+
export { zodRequiredAny, zodTransform, zodUploadFile } from "./zod";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zodUploadFile = exports.zodTransform = exports.zodRequiredAny = exports.RUNTIME = exports.encodeQueryParam = exports.isUploadFile = exports.createForm = exports.CoreResourceClient = exports.CoreClient = exports.BinaryResponse = exports.OAuth2 = exports.AuthKey = exports.AuthBearer = exports.AuthBasic = exports.ApiPromise = exports.ApiError = void 0;
|
|
4
|
+
var api_error_1 = require("./api-error");
|
|
5
|
+
Object.defineProperty(exports, "ApiError", { enumerable: true, get: function () { return api_error_1.ApiError; } });
|
|
6
|
+
var api_promise_1 = require("./api-promise");
|
|
7
|
+
Object.defineProperty(exports, "ApiPromise", { enumerable: true, get: function () { return api_promise_1.ApiPromise; } });
|
|
8
|
+
var auth_1 = require("./auth");
|
|
9
|
+
Object.defineProperty(exports, "AuthBasic", { enumerable: true, get: function () { return auth_1.AuthBasic; } });
|
|
10
|
+
Object.defineProperty(exports, "AuthBearer", { enumerable: true, get: function () { return auth_1.AuthBearer; } });
|
|
11
|
+
Object.defineProperty(exports, "AuthKey", { enumerable: true, get: function () { return auth_1.AuthKey; } });
|
|
12
|
+
Object.defineProperty(exports, "OAuth2", { enumerable: true, get: function () { return auth_1.OAuth2; } });
|
|
13
|
+
var binary_response_1 = require("./binary-response");
|
|
14
|
+
Object.defineProperty(exports, "BinaryResponse", { enumerable: true, get: function () { return binary_response_1.BinaryResponse; } });
|
|
15
|
+
var core_client_1 = require("./core-client");
|
|
16
|
+
Object.defineProperty(exports, "CoreClient", { enumerable: true, get: function () { return core_client_1.CoreClient; } });
|
|
17
|
+
Object.defineProperty(exports, "CoreResourceClient", { enumerable: true, get: function () { return core_client_1.CoreResourceClient; } });
|
|
18
|
+
var form_data_1 = require("./form-data");
|
|
19
|
+
Object.defineProperty(exports, "createForm", { enumerable: true, get: function () { return form_data_1.createForm; } });
|
|
20
|
+
Object.defineProperty(exports, "isUploadFile", { enumerable: true, get: function () { return form_data_1.isUploadFile; } });
|
|
21
|
+
var query_1 = require("./query");
|
|
22
|
+
Object.defineProperty(exports, "encodeQueryParam", { enumerable: true, get: function () { return query_1.encodeQueryParam; } });
|
|
23
|
+
var runtime_1 = require("./runtime");
|
|
24
|
+
Object.defineProperty(exports, "RUNTIME", { enumerable: true, get: function () { return runtime_1.RUNTIME; } });
|
|
25
|
+
var zod_1 = require("./zod");
|
|
26
|
+
Object.defineProperty(exports, "zodRequiredAny", { enumerable: true, get: function () { return zod_1.zodRequiredAny; } });
|
|
27
|
+
Object.defineProperty(exports, "zodTransform", { enumerable: true, get: function () { return zod_1.zodTransform; } });
|
|
28
|
+
Object.defineProperty(exports, "zodUploadFile", { enumerable: true, get: function () { return zod_1.zodUploadFile; } });
|