magic-hour 0.46.0 → 0.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.
- package/dist/environment.d.ts +1 -1
- package/dist/environment.js +1 -1
- package/dist/resources/v1/ai-image-editor/resource-client.d.ts +1 -1
- package/dist/resources/v1/ai-image-editor/resource-client.js +1 -1
- package/dist/resources/v1/ai-image-generator/request-types.d.ts +48 -4
- package/dist/resources/v1/ai-image-generator/request-types.js +30 -2
- package/dist/resources/v1/ai-image-generator/resource-client.d.ts +1 -1
- package/dist/resources/v1/ai-image-generator/resource-client.js +1 -1
- package/dist/resources/v1/image-to-video/request-types.d.ts +29 -7
- package/dist/resources/v1/image-to-video/request-types.js +24 -0
- package/dist/resources/v1/text-to-video/request-types.d.ts +41 -12
- package/dist/resources/v1/text-to-video/request-types.js +30 -2
- package/dist/types/index.d.ts +6 -0
- package/dist/types/v1-ai-image-editor-create-body-style-model-enum.d.ts +2 -1
- package/dist/types/v1-ai-image-editor-create-body-style.d.ts +2 -1
- package/dist/types/v1-ai-image-generator-create-body-aspect-ratio-enum.d.ts +4 -0
- package/dist/types/v1-ai-image-generator-create-body-aspect-ratio-enum.js +2 -0
- package/dist/types/v1-ai-image-generator-create-body-model-enum.d.ts +24 -0
- package/dist/types/v1-ai-image-generator-create-body-model-enum.js +2 -0
- package/dist/types/v1-ai-image-generator-create-body-orientation-enum.d.ts +3 -1
- package/dist/types/v1-ai-image-generator-create-body-resolution-enum.d.ts +11 -0
- package/dist/types/v1-ai-image-generator-create-body-resolution-enum.js +2 -0
- package/dist/types/v1-ai-image-generator-create-body-style-quality-mode-enum.d.ts +5 -5
- package/dist/types/v1-ai-image-generator-create-body-style.d.ts +5 -5
- package/dist/types/v1-ai-image-generator-create-body.d.ts +48 -4
- package/dist/types/v1-ai-image-generator-create-body.js +30 -2
- package/dist/types/v1-ai-talking-photo-create-body-style-generation-mode-enum.d.ts +8 -5
- package/dist/types/v1-ai-talking-photo-create-body-style.d.ts +15 -6
- package/dist/types/v1-ai-talking-photo-create-body-style.js +20 -2
- package/dist/types/v1-image-projects-get-response.d.ts +1 -1
- package/dist/types/v1-image-to-video-create-body-assets.d.ts +1 -1
- package/dist/types/v1-image-to-video-create-body-model-enum.d.ts +11 -0
- package/dist/types/v1-image-to-video-create-body-model-enum.js +2 -0
- package/dist/types/v1-image-to-video-create-body-resolution-enum.d.ts +7 -6
- package/dist/types/v1-image-to-video-create-body.d.ts +29 -7
- package/dist/types/v1-image-to-video-create-body.js +24 -0
- package/dist/types/v1-text-to-video-create-body-aspect-ratio-enum.d.ts +10 -0
- package/dist/types/v1-text-to-video-create-body-aspect-ratio-enum.js +2 -0
- package/dist/types/v1-text-to-video-create-body-model-enum.d.ts +11 -0
- package/dist/types/v1-text-to-video-create-body-model-enum.js +2 -0
- package/dist/types/v1-text-to-video-create-body-orientation-enum.d.ts +1 -1
- package/dist/types/v1-text-to-video-create-body-resolution-enum.d.ts +7 -6
- package/dist/types/v1-text-to-video-create-body.d.ts +41 -12
- package/dist/types/v1-text-to-video-create-body.js +30 -2
- package/dist/types/v1-video-projects-get-response.d.ts +1 -1
- package/package.json +1 -1
- package/dist/core/api-error.d.ts +0 -6
- package/dist/core/api-error.js +0 -11
- package/dist/core/api-promise.d.ts +0 -28
- package/dist/core/api-promise.js +0 -252
- package/dist/core/auth.d.ts +0 -76
- package/dist/core/auth.js +0 -232
- package/dist/core/binary-response.d.ts +0 -6
- package/dist/core/binary-response.js +0 -10
- package/dist/core/content-type.d.ts +0 -4
- package/dist/core/content-type.js +0 -12
- package/dist/core/core-client.d.ts +0 -58
- package/dist/core/core-client.js +0 -168
- package/dist/core/form-data.d.ts +0 -26
- package/dist/core/form-data.js +0 -83
- package/dist/core/index.d.ts +0 -9
- package/dist/core/index.js +0 -28
- package/dist/core/logger.d.ts +0 -48
- package/dist/core/logger.js +0 -82
- package/dist/core/query.d.ts +0 -8
- package/dist/core/query.js +0 -77
- package/dist/core/runtime.d.ts +0 -4
- package/dist/core/runtime.js +0 -20
- package/dist/core/zod.d.ts +0 -13
- package/dist/core/zod.js +0 -60
- package/dist/examples/logging-example.d.ts +0 -10
- package/dist/examples/logging-example.js +0 -137
- package/dist/logging-client.d.ts +0 -39
- package/dist/logging-client.js +0 -116
package/package.json
CHANGED
package/dist/core/api-error.d.ts
DELETED
package/dist/core/api-error.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ApiError = void 0;
|
|
4
|
-
class ApiError extends Error {
|
|
5
|
-
constructor(request, response) {
|
|
6
|
-
super(`${response.status} was returned from ${request.method} ${request.path}`);
|
|
7
|
-
this.request = request;
|
|
8
|
-
this.response = response;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
exports.ApiError = ApiError;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type * as z from "zod";
|
|
2
|
-
import type { ApiResponse } from "./core-client";
|
|
3
|
-
export interface ApiPromiseProps {
|
|
4
|
-
responsePromise: Promise<ApiResponse>;
|
|
5
|
-
responseStream: boolean;
|
|
6
|
-
responseRaw: boolean;
|
|
7
|
-
responseSchema?: z.Schema | undefined;
|
|
8
|
-
}
|
|
9
|
-
export type EventSourceResponse<T> = AsyncIterableIterator<T>;
|
|
10
|
-
export declare class ApiPromise<T> extends Promise<T> implements AsyncIterator<T>, AsyncIterable<T> {
|
|
11
|
-
private responseProps;
|
|
12
|
-
constructor(responseProps: ApiPromiseProps);
|
|
13
|
-
/**
|
|
14
|
-
* Gets the raw response as returned by `fetch` rather than automatically
|
|
15
|
-
* parsing the response.
|
|
16
|
-
*/
|
|
17
|
-
asResponse(): Promise<ApiResponse>;
|
|
18
|
-
private getResponseType;
|
|
19
|
-
private parseResponse;
|
|
20
|
-
private handleNodeStream;
|
|
21
|
-
private handleWebStream;
|
|
22
|
-
asEventStream(): AsyncIterableIterator<T>;
|
|
23
|
-
next(): Promise<IteratorResult<T>>;
|
|
24
|
-
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
25
|
-
then<Result1 = T, Result2 = never>(onfulfilled?: ((value: T) => Result1 | PromiseLike<Result1>) | undefined | null, onrejected?: ((reason: any) => Result2 | PromiseLike<Result2>) | undefined | null): Promise<Result1 | Result2>;
|
|
26
|
-
catch<Result = never>(onrejected?: ((reason: any) => Result | PromiseLike<Result>) | undefined | null): Promise<T | Result>;
|
|
27
|
-
finally(onfinally?: (() => void) | undefined | null): Promise<T>;
|
|
28
|
-
}
|
package/dist/core/api-promise.js
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
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
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
12
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
13
|
-
var m = o[Symbol.asyncIterator], i;
|
|
14
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
15
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
16
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
|
-
};
|
|
18
|
-
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
19
|
-
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
20
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
21
|
-
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
22
|
-
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
23
|
-
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
|
|
24
|
-
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
|
|
25
|
-
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
26
|
-
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
27
|
-
function fulfill(value) { resume("next", value); }
|
|
28
|
-
function reject(value) { resume("throw", value); }
|
|
29
|
-
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
30
|
-
};
|
|
31
|
-
var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
|
|
32
|
-
var i, p;
|
|
33
|
-
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
|
|
34
|
-
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }
|
|
35
|
-
};
|
|
36
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
-
exports.ApiPromise = void 0;
|
|
38
|
-
const binary_response_1 = require("./binary-response");
|
|
39
|
-
const content_type_1 = require("./content-type");
|
|
40
|
-
class ApiPromise extends Promise {
|
|
41
|
-
constructor(responseProps) {
|
|
42
|
-
super((resolve) => {
|
|
43
|
-
resolve(null);
|
|
44
|
-
});
|
|
45
|
-
this.responseProps = responseProps;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Gets the raw response as returned by `fetch` rather than automatically
|
|
49
|
-
* parsing the response.
|
|
50
|
-
*/
|
|
51
|
-
asResponse() {
|
|
52
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
-
return yield this.responseProps.responsePromise;
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
getResponseType(contentType) {
|
|
57
|
-
if (content_type_1.JSON_PATTERN.test(contentType)) {
|
|
58
|
-
return "json";
|
|
59
|
-
}
|
|
60
|
-
else if (content_type_1.TEXT_PATTERN.test(contentType)) {
|
|
61
|
-
return "text";
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
return "binary";
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
parseResponse() {
|
|
68
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
-
var _a;
|
|
70
|
-
const { responsePromise, responseSchema, responseRaw } = this.responseProps;
|
|
71
|
-
const response = yield responsePromise;
|
|
72
|
-
if (response.status === 204) {
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
else if (responseRaw) {
|
|
76
|
-
return response;
|
|
77
|
-
}
|
|
78
|
-
const contentType = (_a = response.headers.get("content-type")) !== null && _a !== void 0 ? _a : "";
|
|
79
|
-
switch (this.getResponseType(contentType)) {
|
|
80
|
-
case "json": {
|
|
81
|
-
const rawJson = yield response.json();
|
|
82
|
-
return responseSchema ? responseSchema.parse(rawJson) : rawJson;
|
|
83
|
-
}
|
|
84
|
-
case "text":
|
|
85
|
-
return (yield response.text());
|
|
86
|
-
case "binary":
|
|
87
|
-
default:
|
|
88
|
-
return new binary_response_1.BinaryResponse(yield response.blob(), contentType);
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
handleNodeStream(stream) {
|
|
93
|
-
return __asyncGenerator(this, arguments, function* handleNodeStream_1() {
|
|
94
|
-
var _a, e_1, _b, _c;
|
|
95
|
-
const { responseSchema } = this.responseProps;
|
|
96
|
-
const parser = new EventSourceParser();
|
|
97
|
-
try {
|
|
98
|
-
for (var _d = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield __await(stream_1.next()), _a = stream_1_1.done, !_a; _d = true) {
|
|
99
|
-
_c = stream_1_1.value;
|
|
100
|
-
_d = false;
|
|
101
|
-
const chunk = _c;
|
|
102
|
-
const text = chunk.toString();
|
|
103
|
-
const messages = parser.parse(text);
|
|
104
|
-
for (const message of messages) {
|
|
105
|
-
if (message.data) {
|
|
106
|
-
const rawJson = { data: JSON.parse(message.data) };
|
|
107
|
-
yield yield __await(responseSchema ? responseSchema.parse(rawJson) : rawJson);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
113
|
-
finally {
|
|
114
|
-
try {
|
|
115
|
-
if (!_d && !_a && (_b = stream_1.return)) yield __await(_b.call(stream_1));
|
|
116
|
-
}
|
|
117
|
-
finally { if (e_1) throw e_1.error; }
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
handleWebStream(stream) {
|
|
122
|
-
return __asyncGenerator(this, arguments, function* handleWebStream_1() {
|
|
123
|
-
const { responseSchema } = this.responseProps;
|
|
124
|
-
const reader = stream.getReader();
|
|
125
|
-
const decoder = new TextDecoder();
|
|
126
|
-
const parser = new EventSourceParser();
|
|
127
|
-
try {
|
|
128
|
-
while (true) {
|
|
129
|
-
const { done, value } = yield __await(reader.read());
|
|
130
|
-
if (done) {
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
const chunk = decoder.decode(value, { stream: true });
|
|
134
|
-
const messages = parser.parse(chunk);
|
|
135
|
-
for (const message of messages) {
|
|
136
|
-
if (message.data) {
|
|
137
|
-
const rawJson = { data: JSON.parse(message.data) };
|
|
138
|
-
yield yield __await(responseSchema ? responseSchema.parse(rawJson) : rawJson);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
finally {
|
|
144
|
-
reader.releaseLock();
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
asEventStream() {
|
|
149
|
-
return __asyncGenerator(this, arguments, function* asEventStream_1() {
|
|
150
|
-
const { responsePromise, responseStream } = this.responseProps;
|
|
151
|
-
if (!responseStream) {
|
|
152
|
-
throw new Error("Response is not an event stream");
|
|
153
|
-
}
|
|
154
|
-
const response = yield __await(responsePromise);
|
|
155
|
-
if (!response.body) {
|
|
156
|
-
throw new Error("Response body is undefined");
|
|
157
|
-
}
|
|
158
|
-
// Check if we're in Node.js environment
|
|
159
|
-
if ("pipe" in response.body && typeof response.body.pipe === "function") {
|
|
160
|
-
yield __await(yield* __asyncDelegator(__asyncValues(this.handleNodeStream(response.body))));
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
yield __await(yield* __asyncDelegator(__asyncValues(this.handleWebStream(response.body))));
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
next() {
|
|
168
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
-
const { responseStream } = this.responseProps;
|
|
170
|
-
if (responseStream) {
|
|
171
|
-
const iterator = this.asEventStream();
|
|
172
|
-
const result = yield iterator.next();
|
|
173
|
-
return result;
|
|
174
|
-
}
|
|
175
|
-
const value = yield this.parseResponse();
|
|
176
|
-
return { value, done: true };
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
[Symbol.asyncIterator]() {
|
|
180
|
-
const iterator = {
|
|
181
|
-
next: () => __awaiter(this, void 0, void 0, function* () {
|
|
182
|
-
const { responseStream } = this.responseProps;
|
|
183
|
-
if (responseStream) {
|
|
184
|
-
const stream = this.asEventStream();
|
|
185
|
-
return stream.next();
|
|
186
|
-
}
|
|
187
|
-
const value = yield this.parseResponse();
|
|
188
|
-
return { value, done: true };
|
|
189
|
-
}),
|
|
190
|
-
};
|
|
191
|
-
return iterator;
|
|
192
|
-
}
|
|
193
|
-
then(onfulfilled, onrejected) {
|
|
194
|
-
if (this.responseProps.responseStream) {
|
|
195
|
-
return Promise.resolve(this.asEventStream());
|
|
196
|
-
}
|
|
197
|
-
return this.parseResponse().then(onfulfilled, onrejected);
|
|
198
|
-
}
|
|
199
|
-
catch(onrejected) {
|
|
200
|
-
return this.parseResponse().catch(onrejected);
|
|
201
|
-
}
|
|
202
|
-
finally(onfinally) {
|
|
203
|
-
return this.parseResponse().finally(onfinally);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
exports.ApiPromise = ApiPromise;
|
|
207
|
-
class EventSourceParser {
|
|
208
|
-
constructor() {
|
|
209
|
-
this.buffer = "";
|
|
210
|
-
}
|
|
211
|
-
parse(chunk) {
|
|
212
|
-
this.buffer += chunk;
|
|
213
|
-
const messages = [];
|
|
214
|
-
const lines = this.buffer.split("\n");
|
|
215
|
-
// Keep the last line in buffer if it's not complete
|
|
216
|
-
this.buffer = lines.pop() || "";
|
|
217
|
-
let currentMessage = {};
|
|
218
|
-
for (const line of lines) {
|
|
219
|
-
if (line.trim() === "") {
|
|
220
|
-
if (currentMessage.data) {
|
|
221
|
-
const message = {
|
|
222
|
-
event: currentMessage.event,
|
|
223
|
-
data: currentMessage.data,
|
|
224
|
-
id: currentMessage.id,
|
|
225
|
-
};
|
|
226
|
-
messages.push(message);
|
|
227
|
-
}
|
|
228
|
-
currentMessage = {};
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
const [fieldName, ...fieldValue] = line.split(":");
|
|
232
|
-
const value = fieldValue.join(":").trim();
|
|
233
|
-
switch (fieldName) {
|
|
234
|
-
case "event":
|
|
235
|
-
currentMessage.event = value;
|
|
236
|
-
break;
|
|
237
|
-
case "data":
|
|
238
|
-
currentMessage.data = currentMessage.data
|
|
239
|
-
? currentMessage.data + "\n" + value
|
|
240
|
-
: value;
|
|
241
|
-
break;
|
|
242
|
-
case "id":
|
|
243
|
-
currentMessage.id = value;
|
|
244
|
-
break;
|
|
245
|
-
case "retry":
|
|
246
|
-
// Handle retry if needed
|
|
247
|
-
break;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
return messages;
|
|
251
|
-
}
|
|
252
|
-
}
|
package/dist/core/auth.d.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import type { RequestConfig } from "./core-client";
|
|
2
|
-
export interface AuthProvider {
|
|
3
|
-
applyAuth: (cfg: RequestConfig) => Promise<RequestConfig>;
|
|
4
|
-
setValue: (val?: string | undefined) => void;
|
|
5
|
-
}
|
|
6
|
-
export declare class AuthBasic implements AuthProvider {
|
|
7
|
-
username: string | null;
|
|
8
|
-
password: string | null;
|
|
9
|
-
constructor(username?: string, password?: string);
|
|
10
|
-
applyAuth(cfg: RequestConfig): Promise<RequestConfig>;
|
|
11
|
-
setValue(val?: string | undefined): void;
|
|
12
|
-
}
|
|
13
|
-
export declare class AuthBearer implements AuthProvider {
|
|
14
|
-
token: string | null;
|
|
15
|
-
constructor(token?: string);
|
|
16
|
-
applyAuth(cfg: RequestConfig): Promise<RequestConfig>;
|
|
17
|
-
setValue(val?: string | undefined): void;
|
|
18
|
-
}
|
|
19
|
-
export declare class AuthKey implements AuthProvider {
|
|
20
|
-
name: string;
|
|
21
|
-
location: "query" | "header" | "cookie";
|
|
22
|
-
key: string | null;
|
|
23
|
-
constructor(name: string, location: "query" | "header" | "cookie", key?: string);
|
|
24
|
-
applyAuth(cfg: RequestConfig): Promise<RequestConfig>;
|
|
25
|
-
setValue(val?: string | undefined): void;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* OAuth2 authentication props for a password flow
|
|
29
|
-
*
|
|
30
|
-
* Details:
|
|
31
|
-
* https://datatracker.ietf.org/doc/html/rfc6749#section-4.3
|
|
32
|
-
*/
|
|
33
|
-
export type OAuth2Password = {
|
|
34
|
-
username: string;
|
|
35
|
-
password: string;
|
|
36
|
-
clientId?: string | undefined;
|
|
37
|
-
clientSecret?: string | undefined;
|
|
38
|
-
grantType?: "password" | string | undefined;
|
|
39
|
-
scope?: string[] | undefined;
|
|
40
|
-
tokenUrl?: string | undefined;
|
|
41
|
-
};
|
|
42
|
-
/**
|
|
43
|
-
* OAuth2 authentication props for a client credentials flow
|
|
44
|
-
*
|
|
45
|
-
* Details:
|
|
46
|
-
* https://datatracker.ietf.org/doc/html/rfc6749#section-4.4
|
|
47
|
-
*/
|
|
48
|
-
export type OAuth2ClientCredentials = {
|
|
49
|
-
clientId: string;
|
|
50
|
-
clientSecret: string;
|
|
51
|
-
grantType?: "client_credentials" | string | undefined;
|
|
52
|
-
scope?: string[] | undefined;
|
|
53
|
-
tokenUrl?: string | undefined;
|
|
54
|
-
};
|
|
55
|
-
export type OAuth2ProviderProps = {
|
|
56
|
-
baseUrl: string;
|
|
57
|
-
defaultTokenUrl: string;
|
|
58
|
-
accessTokenPointer: string;
|
|
59
|
-
expiresInPointer: string;
|
|
60
|
-
credentialsLocation: "request_body" | "basic_authorization_header";
|
|
61
|
-
bodyContent: "form" | "json";
|
|
62
|
-
requestMutator: AuthProvider;
|
|
63
|
-
form?: OAuth2Password | OAuth2ClientCredentials | undefined;
|
|
64
|
-
};
|
|
65
|
-
export declare class OAuth2 implements AuthProvider {
|
|
66
|
-
props: OAuth2ProviderProps;
|
|
67
|
-
accessToken?: string | undefined;
|
|
68
|
-
expiresAt?: Date;
|
|
69
|
-
constructor(props: OAuth2ProviderProps);
|
|
70
|
-
refresh(form: OAuth2Password | OAuth2ClientCredentials): Promise<{
|
|
71
|
-
accessToken: string;
|
|
72
|
-
expiresAt: Date;
|
|
73
|
-
}>;
|
|
74
|
-
applyAuth(cfg: RequestConfig): Promise<RequestConfig>;
|
|
75
|
-
setValue(val?: string | undefined): void;
|
|
76
|
-
}
|
package/dist/core/auth.js
DELETED
|
@@ -1,232 +0,0 @@
|
|
|
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;
|
|
@@ -1,10 +0,0 @@
|
|
|
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;
|
|
@@ -1,12 +0,0 @@
|
|
|
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";
|