@unkey/api 0.6.11 → 0.6.13
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/index.d.mts +36 -9
- package/dist/index.d.ts +36 -9
- package/dist/index.js +18 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +18 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
type ErrorCode = "NOT_FOUND" | "BAD_REQUEST" | "UNAUTHORIZED" | "INTERNAL_SERVER_ERROR" | "RATELIMITED" | "FORBIDDEN" | "KEY_USAGE_EXCEEDED" | "INVALID_KEY_TYPE" | "NOT_UNIQUE";
|
|
2
|
+
type UnkeyError = {
|
|
3
|
+
code: ErrorCode;
|
|
4
|
+
docs: string;
|
|
5
|
+
message: string;
|
|
6
|
+
requestId: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
1
9
|
type UnkeyOptions = {
|
|
2
10
|
/**
|
|
3
11
|
* @default https://api.unkey.dev
|
|
@@ -8,6 +16,13 @@ type UnkeyOptions = {
|
|
|
8
16
|
*/
|
|
9
17
|
token: string;
|
|
10
18
|
};
|
|
19
|
+
type Result<R> = {
|
|
20
|
+
result: R;
|
|
21
|
+
error?: never;
|
|
22
|
+
} | {
|
|
23
|
+
result?: never;
|
|
24
|
+
error: UnkeyError;
|
|
25
|
+
};
|
|
11
26
|
declare class Unkey {
|
|
12
27
|
readonly baseUrl: string;
|
|
13
28
|
private readonly token;
|
|
@@ -89,10 +104,10 @@ declare class Unkey {
|
|
|
89
104
|
* @see https://docs.unkey.dev/features/remaining
|
|
90
105
|
*/
|
|
91
106
|
remaining?: number;
|
|
92
|
-
}) => Promise<{
|
|
107
|
+
}) => Promise<Result<{
|
|
93
108
|
key: string;
|
|
94
109
|
keyId: string;
|
|
95
|
-
}
|
|
110
|
+
}>>;
|
|
96
111
|
update: (req: {
|
|
97
112
|
/**
|
|
98
113
|
* The id of the key to update.
|
|
@@ -152,13 +167,13 @@ declare class Unkey {
|
|
|
152
167
|
* @see https://docs.unkey.dev/features/remaining
|
|
153
168
|
*/
|
|
154
169
|
remaining?: number | null;
|
|
155
|
-
}) => Promise<{
|
|
170
|
+
}) => Promise<Result<{
|
|
156
171
|
key: string;
|
|
157
172
|
keyId: string;
|
|
158
|
-
}
|
|
173
|
+
}>>;
|
|
159
174
|
verify: (req: {
|
|
160
175
|
key: string;
|
|
161
|
-
}) => Promise<{
|
|
176
|
+
}) => Promise<Result<{
|
|
162
177
|
/**
|
|
163
178
|
* Whether or not this key is valid and has passed the ratelimit. If false you should not grant access to whatever the user is requesting
|
|
164
179
|
*/
|
|
@@ -167,11 +182,23 @@ declare class Unkey {
|
|
|
167
182
|
* If you have set an ownerId on this key it is returned here. You can use this to clearly authenticate a user in your system.
|
|
168
183
|
*/
|
|
169
184
|
ownerId?: string;
|
|
185
|
+
/**
|
|
186
|
+
* This is the meta data you have set when creating the key.
|
|
187
|
+
*
|
|
188
|
+
* Example:
|
|
189
|
+
*
|
|
190
|
+
* ```json
|
|
191
|
+
* {
|
|
192
|
+
* "billingTier":"PRO",
|
|
193
|
+
* "trialEnds": "2023-06-16T17:16:37.161Z"
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
170
197
|
meta?: unknown;
|
|
171
|
-
}
|
|
198
|
+
}>>;
|
|
172
199
|
revoke: (req: {
|
|
173
200
|
keyId: string;
|
|
174
|
-
}) => Promise<void
|
|
201
|
+
}) => Promise<Result<void>>;
|
|
175
202
|
};
|
|
176
203
|
/**
|
|
177
204
|
* Must be authenticated via app token
|
|
@@ -189,10 +216,10 @@ declare class Unkey {
|
|
|
189
216
|
*/
|
|
190
217
|
expires?: number;
|
|
191
218
|
forWorkspaceId: string;
|
|
192
|
-
}) => Promise<{
|
|
219
|
+
}) => Promise<Result<{
|
|
193
220
|
key: string;
|
|
194
221
|
keyId: string;
|
|
195
|
-
}
|
|
222
|
+
}>>;
|
|
196
223
|
};
|
|
197
224
|
}
|
|
198
225
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
type ErrorCode = "NOT_FOUND" | "BAD_REQUEST" | "UNAUTHORIZED" | "INTERNAL_SERVER_ERROR" | "RATELIMITED" | "FORBIDDEN" | "KEY_USAGE_EXCEEDED" | "INVALID_KEY_TYPE" | "NOT_UNIQUE";
|
|
2
|
+
type UnkeyError = {
|
|
3
|
+
code: ErrorCode;
|
|
4
|
+
docs: string;
|
|
5
|
+
message: string;
|
|
6
|
+
requestId: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
1
9
|
type UnkeyOptions = {
|
|
2
10
|
/**
|
|
3
11
|
* @default https://api.unkey.dev
|
|
@@ -8,6 +16,13 @@ type UnkeyOptions = {
|
|
|
8
16
|
*/
|
|
9
17
|
token: string;
|
|
10
18
|
};
|
|
19
|
+
type Result<R> = {
|
|
20
|
+
result: R;
|
|
21
|
+
error?: never;
|
|
22
|
+
} | {
|
|
23
|
+
result?: never;
|
|
24
|
+
error: UnkeyError;
|
|
25
|
+
};
|
|
11
26
|
declare class Unkey {
|
|
12
27
|
readonly baseUrl: string;
|
|
13
28
|
private readonly token;
|
|
@@ -89,10 +104,10 @@ declare class Unkey {
|
|
|
89
104
|
* @see https://docs.unkey.dev/features/remaining
|
|
90
105
|
*/
|
|
91
106
|
remaining?: number;
|
|
92
|
-
}) => Promise<{
|
|
107
|
+
}) => Promise<Result<{
|
|
93
108
|
key: string;
|
|
94
109
|
keyId: string;
|
|
95
|
-
}
|
|
110
|
+
}>>;
|
|
96
111
|
update: (req: {
|
|
97
112
|
/**
|
|
98
113
|
* The id of the key to update.
|
|
@@ -152,13 +167,13 @@ declare class Unkey {
|
|
|
152
167
|
* @see https://docs.unkey.dev/features/remaining
|
|
153
168
|
*/
|
|
154
169
|
remaining?: number | null;
|
|
155
|
-
}) => Promise<{
|
|
170
|
+
}) => Promise<Result<{
|
|
156
171
|
key: string;
|
|
157
172
|
keyId: string;
|
|
158
|
-
}
|
|
173
|
+
}>>;
|
|
159
174
|
verify: (req: {
|
|
160
175
|
key: string;
|
|
161
|
-
}) => Promise<{
|
|
176
|
+
}) => Promise<Result<{
|
|
162
177
|
/**
|
|
163
178
|
* Whether or not this key is valid and has passed the ratelimit. If false you should not grant access to whatever the user is requesting
|
|
164
179
|
*/
|
|
@@ -167,11 +182,23 @@ declare class Unkey {
|
|
|
167
182
|
* If you have set an ownerId on this key it is returned here. You can use this to clearly authenticate a user in your system.
|
|
168
183
|
*/
|
|
169
184
|
ownerId?: string;
|
|
185
|
+
/**
|
|
186
|
+
* This is the meta data you have set when creating the key.
|
|
187
|
+
*
|
|
188
|
+
* Example:
|
|
189
|
+
*
|
|
190
|
+
* ```json
|
|
191
|
+
* {
|
|
192
|
+
* "billingTier":"PRO",
|
|
193
|
+
* "trialEnds": "2023-06-16T17:16:37.161Z"
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
170
197
|
meta?: unknown;
|
|
171
|
-
}
|
|
198
|
+
}>>;
|
|
172
199
|
revoke: (req: {
|
|
173
200
|
keyId: string;
|
|
174
|
-
}) => Promise<void
|
|
201
|
+
}) => Promise<Result<void>>;
|
|
175
202
|
};
|
|
176
203
|
/**
|
|
177
204
|
* Must be authenticated via app token
|
|
@@ -189,10 +216,10 @@ declare class Unkey {
|
|
|
189
216
|
*/
|
|
190
217
|
expires?: number;
|
|
191
218
|
forWorkspaceId: string;
|
|
192
|
-
}) => Promise<{
|
|
219
|
+
}) => Promise<Result<{
|
|
193
220
|
key: string;
|
|
194
221
|
keyId: string;
|
|
195
|
-
}
|
|
222
|
+
}>>;
|
|
196
223
|
};
|
|
197
224
|
}
|
|
198
225
|
|
package/dist/index.js
CHANGED
|
@@ -34,19 +34,24 @@ var Unkey = class {
|
|
|
34
34
|
this.token = opts.token;
|
|
35
35
|
}
|
|
36
36
|
async fetch(req) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
37
|
+
try {
|
|
38
|
+
const url = `${this.baseUrl}/${req.path.join("/")}`;
|
|
39
|
+
const res = await fetch(url, {
|
|
40
|
+
method: req.method,
|
|
41
|
+
headers: {
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
Authorization: `Bearer ${this.token}`
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify(req.body)
|
|
46
|
+
});
|
|
47
|
+
if (!res.ok) {
|
|
48
|
+
return { error: await res.json() };
|
|
49
|
+
}
|
|
50
|
+
return await res.json();
|
|
51
|
+
} catch (e) {
|
|
52
|
+
console.error(e);
|
|
53
|
+
throw e;
|
|
48
54
|
}
|
|
49
|
-
return await res.json();
|
|
50
55
|
}
|
|
51
56
|
get keys() {
|
|
52
57
|
return {
|
|
@@ -72,7 +77,7 @@ var Unkey = class {
|
|
|
72
77
|
});
|
|
73
78
|
},
|
|
74
79
|
revoke: async (req) => {
|
|
75
|
-
await this.fetch({
|
|
80
|
+
return await this.fetch({
|
|
76
81
|
path: ["v1", "keys", req.keyId],
|
|
77
82
|
method: "DELETE"
|
|
78
83
|
});
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type UnkeyOptions = {\n /**\n * @default https://api.unkey.dev\n */\n baseUrl?: string;\n\n /**\n * The workspace key from unkey.dev\n */\n token: string;\n};\n\ntype ApiRequest = {\n path: string[];\n method: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n body?: unknown;\n};\n\nexport class Unkey {\n public readonly baseUrl: string;\n private readonly token: string;\n\n constructor(opts: UnkeyOptions) {\n this.baseUrl = opts.baseUrl ?? \"https://api.unkey.dev\";\n /**\n * Even though typescript should prevent this, some people still pass undefined or empty strings\n */\n if (!opts.token) {\n throw new Error(\"Unkey token must not be empty\");\n }\n this.token = opts.token;\n }\n\n private async fetch<TResult>(req: ApiRequest): Promise<TResult> {\n const url = `${this.baseUrl}/${req.path.join(\"/\")}`;\n const res = await fetch(url, {\n method: req.method,\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.token}`,\n },\n body: JSON.stringify(req.body),\n });\n if (!res.ok) {\n throw new Error(await res.text());\n }\n return await res.json();\n }\n\n public get keys() {\n return {\n create: async (req: {\n /**\n * Provide a name to this key if you want for later reference\n */\n name?: string;\n /**\n * Choose an API where this key should be created.\n */\n apiId: string;\n /**\n * To make it easier for your users to understand which product an api key belongs to, you can add prefix them.\n *\n * For example Stripe famously prefixes their customer ids with cus_ or their api keys with sk_live_.\n *\n * The underscore is automtically added if you are defining a prefix, for example: \"prefix\": \"abc\" will result in a key like abc_xxxxxxxxx\n */\n prefix?: string;\n\n /**\n * The bytelength used to generate your key determines its entropy as well as its length. Higher is better, but keys become longer and more annoying to handle.\n *\n * The default is 16 bytes, or 2128 possible combinations\n */\n byteLength?: number;\n /**\n * Your user’s Id. This will provide a link between Unkey and your customer record.\n *\n * When validating a key, we will return this back to you, so you can clearly identify your user from their api key.\n */\n ownerId?: string;\n /**\n * This is a place for dynamic meta data, anything that feels useful for you should go here\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n meta?: unknown;\n /**\n * You can auto expire keys by providing a unix timestamp in milliseconds.\n *\n * Once keys expire they will automatically be deleted and are no longer valid.\n */\n expires?: number;\n\n /**\n * Unkey comes with per-key ratelimiting out of the box.\n *\n * @see https://docs.unkey.dev/features/ratelimiting\n */\n ratelimit?: {\n type: \"fast\" | \"consistent\";\n /**\n * The total amount of burstable requests.\n */\n limit: number;\n\n /**\n * How many tokens to refill during each refillInterval\n */\n refillRate: number;\n\n /**\n * Determines the speed at which tokens are refilled.\n * In milliseconds.\n */\n refillInterval: number;\n };\n\n /**\n * Unkey allows you to set/update usage limits on individual keys\n *\n * @see https://docs.unkey.dev/features/remaining\n */\n remaining?: number;\n }): Promise<{ key: string; keyId: string }> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"keys\"],\n method: \"POST\",\n body: req,\n });\n },\n update: async (req: {\n /**\n * The id of the key to update.\n */\n keyId: string;\n /**\n * Update the name\n */\n name?: string | null;\n\n /**\n * Update the owner id\n */\n ownerId?: string | null;\n /**\n * This is a place for dynamic meta data, anything that feels useful for you should go here\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n meta?: unknown | null;\n /**\n * Update the expiration time, Unix timstamp in milliseconds\n *\n *\n */\n expires?: number | null;\n\n /**\n * Update the ratelimit\n *\n * @see https://docs.unkey.dev/features/ratelimiting\n */\n ratelimit?: {\n type: \"fast\" | \"consistent\";\n /**\n * The total amount of burstable requests.\n */\n limit: number;\n\n /**\n * How many tokens to refill during each refillInterval\n */\n refillRate: number;\n\n /**\n * Determines the speed at which tokens are refilled.\n * In milliseconds.\n */\n refillInterval: number;\n } | null;\n\n /**\n * Update the remaining verifications.\n *\n * @see https://docs.unkey.dev/features/remaining\n */\n remaining?: number | null;\n }): Promise<{ key: string; keyId: string }> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"keys\", req.keyId],\n method: \"PUT\",\n body: req,\n });\n },\n verify: async (req: { key: string }): Promise<{\n /**\n * Whether or not this key is valid and has passed the ratelimit. If false you should not grant access to whatever the user is requesting\n */\n valid: boolean;\n\n /**\n * If you have set an ownerId on this key it is returned here. You can use this to clearly authenticate a user in your system.\n */\n ownerId?: string;\n\n meta?: unknown;\n\n /**\n * This is the meta data you have set when creating the key.\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n }> => {\n return await this.fetch<{\n valid: boolean;\n ownerId?: string;\n meta?: unknown;\n }>({\n path: [\"v1\", \"keys\", \"verify\"],\n method: \"POST\",\n body: req,\n });\n },\n revoke: async (req: { keyId: string }): Promise<void> => {\n await this.fetch<{\n valid: boolean;\n ownerId?: string;\n meta?: unknown;\n }>({\n path: [\"v1\", \"keys\", req.keyId],\n method: \"DELETE\",\n });\n },\n };\n }\n /**\n * Must be authenticated via app token\n */\n public get _internal() {\n return {\n createRootKey: async (req: {\n /**\n * Provide a name to this key if you want for later reference\n */\n name?: string;\n\n /**\n * You can auto expire keys by providing a unix timestamp in milliseconds.\n *\n * Once keys expire they will automatically be deleted and are no longer valid.\n */\n expires?: number;\n\n // Used to create root keys from the frontend, please ignore\n forWorkspaceId: string;\n }): Promise<{ key: string; keyId: string }> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"internal\", \"rootkeys\"],\n method: \"POST\",\n body: req,\n });\n },\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBO,IAAM,QAAN,MAAY;AAAA,EACD;AAAA,EACC;AAAA,EAEjB,YAAY,MAAoB;AAC9B,SAAK,UAAU,KAAK,WAAW;AAI/B,QAAI,CAAC,KAAK,OAAO;AACf,YAAM,IAAI,MAAM,+BAA+B;AAAA,IACjD;AACA,SAAK,QAAQ,KAAK;AAAA,EACpB;AAAA,EAEA,MAAc,MAAe,KAAmC;AAC9D,UAAM,MAAM,GAAG,KAAK,OAAO,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC;AACjD,UAAM,MAAM,MAAM,MAAM,KAAK;AAAA,MAC3B,QAAQ,IAAI;AAAA,MACZ,SAAS;AAAA,QACP,gBAAgB;AAAA,QAChB,eAAe,UAAU,KAAK,KAAK;AAAA,MACrC;AAAA,MACA,MAAM,KAAK,UAAU,IAAI,IAAI;AAAA,IAC/B,CAAC;AACD,QAAI,CAAC,IAAI,IAAI;AACX,YAAM,IAAI,MAAM,MAAM,IAAI,KAAK,CAAC;AAAA,IAClC;AACA,WAAO,MAAM,IAAI,KAAK;AAAA,EACxB;AAAA,EAEA,IAAW,OAAO;AAChB,WAAO;AAAA,MACL,QAAQ,OAAO,QAgFgC;AAC7C,eAAO,MAAM,KAAK,MAAsC;AAAA,UACtD,MAAM,CAAC,MAAM,MAAM;AAAA,UACnB,QAAQ;AAAA,UACR,MAAM;AAAA,QACR,CAAC;AAAA,MACH;AAAA,MACA,QAAQ,OAAO,QAgEgC;AAC7C,eAAO,MAAM,KAAK,MAAsC;AAAA,UACtD,MAAM,CAAC,MAAM,QAAQ,IAAI,KAAK;AAAA,UAC9B,QAAQ;AAAA,UACR,MAAM;AAAA,QACR,CAAC;AAAA,MACH;AAAA,MACA,QAAQ,OAAO,QAyBT;AACJ,eAAO,MAAM,KAAK,MAIf;AAAA,UACD,MAAM,CAAC,MAAM,QAAQ,QAAQ;AAAA,UAC7B,QAAQ;AAAA,UACR,MAAM;AAAA,QACR,CAAC;AAAA,MACH;AAAA,MACA,QAAQ,OAAO,QAA0C;AACvD,cAAM,KAAK,MAIR;AAAA,UACD,MAAM,CAAC,MAAM,QAAQ,IAAI,KAAK;AAAA,UAC9B,QAAQ;AAAA,QACV,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAIA,IAAW,YAAY;AACrB,WAAO;AAAA,MACL,eAAe,OAAO,QAeyB;AAC7C,eAAO,MAAM,KAAK,MAAsC;AAAA,UACtD,MAAM,CAAC,MAAM,YAAY,UAAU;AAAA,UACnC,QAAQ;AAAA,UACR,MAAM;AAAA,QACR,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { UnkeyError } from \"./errors\";\n\nexport type UnkeyOptions = {\n /**\n * @default https://api.unkey.dev\n */\n baseUrl?: string;\n\n /**\n * The workspace key from unkey.dev\n */\n token: string;\n};\n\ntype ApiRequest = {\n path: string[];\n method: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n body?: unknown;\n};\n\ntype Result<R> =\n | {\n result: R;\n error?: never;\n }\n | {\n result?: never;\n error: UnkeyError;\n };\n\nexport class Unkey {\n public readonly baseUrl: string;\n private readonly token: string;\n\n constructor(opts: UnkeyOptions) {\n this.baseUrl = opts.baseUrl ?? \"https://api.unkey.dev\";\n /**\n * Even though typescript should prevent this, some people still pass undefined or empty strings\n */\n if (!opts.token) {\n throw new Error(\"Unkey token must not be empty\");\n }\n this.token = opts.token;\n }\n\n private async fetch<TResult>(req: ApiRequest): Promise<Result<TResult>> {\n try {\n const url = `${this.baseUrl}/${req.path.join(\"/\")}`;\n const res = await fetch(url, {\n method: req.method,\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.token}`,\n },\n body: JSON.stringify(req.body),\n });\n if (!res.ok) {\n return { error: await res.json() };\n }\n return await res.json();\n } catch (e) {\n console.error(e);\n throw e;\n }\n }\n\n public get keys() {\n return {\n create: async (req: {\n /**\n * Provide a name to this key if you want for later reference\n */\n name?: string;\n /**\n * Choose an API where this key should be created.\n */\n apiId: string;\n /**\n * To make it easier for your users to understand which product an api key belongs to, you can add prefix them.\n *\n * For example Stripe famously prefixes their customer ids with cus_ or their api keys with sk_live_.\n *\n * The underscore is automtically added if you are defining a prefix, for example: \"prefix\": \"abc\" will result in a key like abc_xxxxxxxxx\n */\n prefix?: string;\n\n /**\n * The bytelength used to generate your key determines its entropy as well as its length. Higher is better, but keys become longer and more annoying to handle.\n *\n * The default is 16 bytes, or 2128 possible combinations\n */\n byteLength?: number;\n /**\n * Your user’s Id. This will provide a link between Unkey and your customer record.\n *\n * When validating a key, we will return this back to you, so you can clearly identify your user from their api key.\n */\n ownerId?: string;\n /**\n * This is a place for dynamic meta data, anything that feels useful for you should go here\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n meta?: unknown;\n /**\n * You can auto expire keys by providing a unix timestamp in milliseconds.\n *\n * Once keys expire they will automatically be deleted and are no longer valid.\n */\n expires?: number;\n\n /**\n * Unkey comes with per-key ratelimiting out of the box.\n *\n * @see https://docs.unkey.dev/features/ratelimiting\n */\n ratelimit?: {\n type: \"fast\" | \"consistent\";\n /**\n * The total amount of burstable requests.\n */\n limit: number;\n\n /**\n * How many tokens to refill during each refillInterval\n */\n refillRate: number;\n\n /**\n * Determines the speed at which tokens are refilled.\n * In milliseconds.\n */\n refillInterval: number;\n };\n\n /**\n * Unkey allows you to set/update usage limits on individual keys\n *\n * @see https://docs.unkey.dev/features/remaining\n */\n remaining?: number;\n }): Promise<Result<{ key: string; keyId: string }>> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"keys\"],\n method: \"POST\",\n body: req,\n });\n },\n update: async (req: {\n /**\n * The id of the key to update.\n */\n keyId: string;\n /**\n * Update the name\n */\n name?: string | null;\n\n /**\n * Update the owner id\n */\n ownerId?: string | null;\n /**\n * This is a place for dynamic meta data, anything that feels useful for you should go here\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n meta?: unknown | null;\n /**\n * Update the expiration time, Unix timstamp in milliseconds\n *\n *\n */\n expires?: number | null;\n\n /**\n * Update the ratelimit\n *\n * @see https://docs.unkey.dev/features/ratelimiting\n */\n ratelimit?: {\n type: \"fast\" | \"consistent\";\n /**\n * The total amount of burstable requests.\n */\n limit: number;\n\n /**\n * How many tokens to refill during each refillInterval\n */\n refillRate: number;\n\n /**\n * Determines the speed at which tokens are refilled.\n * In milliseconds.\n */\n refillInterval: number;\n } | null;\n\n /**\n * Update the remaining verifications.\n *\n * @see https://docs.unkey.dev/features/remaining\n */\n remaining?: number | null;\n }): Promise<Result<{ key: string; keyId: string }>> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"keys\", req.keyId],\n method: \"PUT\",\n body: req,\n });\n },\n verify: async (req: { key: string }): Promise<\n Result<{\n /**\n * Whether or not this key is valid and has passed the ratelimit. If false you should not grant access to whatever the user is requesting\n */\n valid: boolean;\n\n /**\n * If you have set an ownerId on this key it is returned here. You can use this to clearly authenticate a user in your system.\n */\n ownerId?: string;\n\n /**\n * This is the meta data you have set when creating the key.\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n meta?: unknown;\n }>\n > => {\n return await this.fetch<{\n valid: boolean;\n ownerId?: string;\n meta?: unknown;\n }>({\n path: [\"v1\", \"keys\", \"verify\"],\n method: \"POST\",\n body: req,\n });\n },\n revoke: async (req: { keyId: string }): Promise<Result<void>> => {\n return await this.fetch<void>({\n path: [\"v1\", \"keys\", req.keyId],\n method: \"DELETE\",\n });\n },\n };\n }\n /**\n * Must be authenticated via app token\n */\n public get _internal() {\n return {\n createRootKey: async (req: {\n /**\n * Provide a name to this key if you want for later reference\n */\n name?: string;\n\n /**\n * You can auto expire keys by providing a unix timestamp in milliseconds.\n *\n * Once keys expire they will automatically be deleted and are no longer valid.\n */\n expires?: number;\n\n // Used to create root keys from the frontend, please ignore\n forWorkspaceId: string;\n }): Promise<Result<{ key: string; keyId: string }>> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"internal\", \"rootkeys\"],\n method: \"POST\",\n body: req,\n });\n },\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA8BO,IAAM,QAAN,MAAY;AAAA,EACD;AAAA,EACC;AAAA,EAEjB,YAAY,MAAoB;AAC9B,SAAK,UAAU,KAAK,WAAW;AAI/B,QAAI,CAAC,KAAK,OAAO;AACf,YAAM,IAAI,MAAM,+BAA+B;AAAA,IACjD;AACA,SAAK,QAAQ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AAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAIA,IAAW,YAAY;AACrB,WAAO;AAAA,MACL,eAAe,OAAO,QAeiC;AACrD,eAAO,MAAM,KAAK,MAAsC;AAAA,UACtD,MAAM,CAAC,MAAM,YAAY,UAAU;AAAA,UACnC,QAAQ;AAAA,UACR,MAAM;AAAA,QACR,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -10,19 +10,24 @@ var Unkey = class {
|
|
|
10
10
|
this.token = opts.token;
|
|
11
11
|
}
|
|
12
12
|
async fetch(req) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
try {
|
|
14
|
+
const url = `${this.baseUrl}/${req.path.join("/")}`;
|
|
15
|
+
const res = await fetch(url, {
|
|
16
|
+
method: req.method,
|
|
17
|
+
headers: {
|
|
18
|
+
"Content-Type": "application/json",
|
|
19
|
+
Authorization: `Bearer ${this.token}`
|
|
20
|
+
},
|
|
21
|
+
body: JSON.stringify(req.body)
|
|
22
|
+
});
|
|
23
|
+
if (!res.ok) {
|
|
24
|
+
return { error: await res.json() };
|
|
25
|
+
}
|
|
26
|
+
return await res.json();
|
|
27
|
+
} catch (e) {
|
|
28
|
+
console.error(e);
|
|
29
|
+
throw e;
|
|
24
30
|
}
|
|
25
|
-
return await res.json();
|
|
26
31
|
}
|
|
27
32
|
get keys() {
|
|
28
33
|
return {
|
|
@@ -48,7 +53,7 @@ var Unkey = class {
|
|
|
48
53
|
});
|
|
49
54
|
},
|
|
50
55
|
revoke: async (req) => {
|
|
51
|
-
await this.fetch({
|
|
56
|
+
return await this.fetch({
|
|
52
57
|
path: ["v1", "keys", req.keyId],
|
|
53
58
|
method: "DELETE"
|
|
54
59
|
});
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type UnkeyOptions = {\n /**\n * @default https://api.unkey.dev\n */\n baseUrl?: string;\n\n /**\n * The workspace key from unkey.dev\n */\n token: string;\n};\n\ntype ApiRequest = {\n path: string[];\n method: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n body?: unknown;\n};\n\nexport class Unkey {\n public readonly baseUrl: string;\n private readonly token: string;\n\n constructor(opts: UnkeyOptions) {\n this.baseUrl = opts.baseUrl ?? \"https://api.unkey.dev\";\n /**\n * Even though typescript should prevent this, some people still pass undefined or empty strings\n */\n if (!opts.token) {\n throw new Error(\"Unkey token must not be empty\");\n }\n this.token = opts.token;\n }\n\n private async fetch<TResult>(req: ApiRequest): Promise<TResult> {\n const url = `${this.baseUrl}/${req.path.join(\"/\")}`;\n const res = await fetch(url, {\n method: req.method,\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.token}`,\n },\n body: JSON.stringify(req.body),\n });\n if (!res.ok) {\n throw new Error(await res.text());\n }\n return await res.json();\n }\n\n public get keys() {\n return {\n create: async (req: {\n /**\n * Provide a name to this key if you want for later reference\n */\n name?: string;\n /**\n * Choose an API where this key should be created.\n */\n apiId: string;\n /**\n * To make it easier for your users to understand which product an api key belongs to, you can add prefix them.\n *\n * For example Stripe famously prefixes their customer ids with cus_ or their api keys with sk_live_.\n *\n * The underscore is automtically added if you are defining a prefix, for example: \"prefix\": \"abc\" will result in a key like abc_xxxxxxxxx\n */\n prefix?: string;\n\n /**\n * The bytelength used to generate your key determines its entropy as well as its length. Higher is better, but keys become longer and more annoying to handle.\n *\n * The default is 16 bytes, or 2128 possible combinations\n */\n byteLength?: number;\n /**\n * Your user’s Id. This will provide a link between Unkey and your customer record.\n *\n * When validating a key, we will return this back to you, so you can clearly identify your user from their api key.\n */\n ownerId?: string;\n /**\n * This is a place for dynamic meta data, anything that feels useful for you should go here\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n meta?: unknown;\n /**\n * You can auto expire keys by providing a unix timestamp in milliseconds.\n *\n * Once keys expire they will automatically be deleted and are no longer valid.\n */\n expires?: number;\n\n /**\n * Unkey comes with per-key ratelimiting out of the box.\n *\n * @see https://docs.unkey.dev/features/ratelimiting\n */\n ratelimit?: {\n type: \"fast\" | \"consistent\";\n /**\n * The total amount of burstable requests.\n */\n limit: number;\n\n /**\n * How many tokens to refill during each refillInterval\n */\n refillRate: number;\n\n /**\n * Determines the speed at which tokens are refilled.\n * In milliseconds.\n */\n refillInterval: number;\n };\n\n /**\n * Unkey allows you to set/update usage limits on individual keys\n *\n * @see https://docs.unkey.dev/features/remaining\n */\n remaining?: number;\n }): Promise<{ key: string; keyId: string }> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"keys\"],\n method: \"POST\",\n body: req,\n });\n },\n update: async (req: {\n /**\n * The id of the key to update.\n */\n keyId: string;\n /**\n * Update the name\n */\n name?: string | null;\n\n /**\n * Update the owner id\n */\n ownerId?: string | null;\n /**\n * This is a place for dynamic meta data, anything that feels useful for you should go here\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n meta?: unknown | null;\n /**\n * Update the expiration time, Unix timstamp in milliseconds\n *\n *\n */\n expires?: number | null;\n\n /**\n * Update the ratelimit\n *\n * @see https://docs.unkey.dev/features/ratelimiting\n */\n ratelimit?: {\n type: \"fast\" | \"consistent\";\n /**\n * The total amount of burstable requests.\n */\n limit: number;\n\n /**\n * How many tokens to refill during each refillInterval\n */\n refillRate: number;\n\n /**\n * Determines the speed at which tokens are refilled.\n * In milliseconds.\n */\n refillInterval: number;\n } | null;\n\n /**\n * Update the remaining verifications.\n *\n * @see https://docs.unkey.dev/features/remaining\n */\n remaining?: number | null;\n }): Promise<{ key: string; keyId: string }> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"keys\", req.keyId],\n method: \"PUT\",\n body: req,\n });\n },\n verify: async (req: { key: string }): Promise<{\n /**\n * Whether or not this key is valid and has passed the ratelimit. If false you should not grant access to whatever the user is requesting\n */\n valid: boolean;\n\n /**\n * If you have set an ownerId on this key it is returned here. You can use this to clearly authenticate a user in your system.\n */\n ownerId?: string;\n\n meta?: unknown;\n\n /**\n * This is the meta data you have set when creating the key.\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n }> => {\n return await this.fetch<{\n valid: boolean;\n ownerId?: string;\n meta?: unknown;\n }>({\n path: [\"v1\", \"keys\", \"verify\"],\n method: \"POST\",\n body: req,\n });\n },\n revoke: async (req: { keyId: string }): Promise<void> => {\n await this.fetch<{\n valid: boolean;\n ownerId?: string;\n meta?: unknown;\n }>({\n path: [\"v1\", \"keys\", req.keyId],\n method: \"DELETE\",\n });\n },\n };\n }\n /**\n * Must be authenticated via app token\n */\n public get _internal() {\n return {\n createRootKey: async (req: {\n /**\n * Provide a name to this key if you want for later reference\n */\n name?: string;\n\n /**\n * You can auto expire keys by providing a unix timestamp in milliseconds.\n *\n * Once keys expire they will automatically be deleted and are no longer valid.\n */\n expires?: number;\n\n // Used to create root keys from the frontend, please ignore\n forWorkspaceId: string;\n }): Promise<{ key: string; keyId: string }> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"internal\", \"rootkeys\"],\n method: \"POST\",\n body: req,\n });\n },\n };\n }\n}\n"],"mappings":";AAkBO,IAAM,QAAN,MAAY;AAAA,EACD;AAAA,EACC;AAAA,EAEjB,YAAY,MAAoB;AAC9B,SAAK,UAAU,KAAK,WAAW;AAI/B,QAAI,CAAC,KAAK,OAAO;AACf,YAAM,IAAI,MAAM,+BAA+B;AAAA,IACjD;AAC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W,YAAY;AACrB,WAAO;AAAA,MACL,eAAe,OAAO,QAeyB;AAC7C,eAAO,MAAM,KAAK,MAAsC;AAAA,UACtD,MAAM,CAAC,MAAM,YAAY,UAAU;AAAA,UACnC,QAAQ;AAAA,UACR,MAAM;AAAA,QACR,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { UnkeyError } from \"./errors\";\n\nexport type UnkeyOptions = {\n /**\n * @default https://api.unkey.dev\n */\n baseUrl?: string;\n\n /**\n * The workspace key from unkey.dev\n */\n token: string;\n};\n\ntype ApiRequest = {\n path: string[];\n method: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n body?: unknown;\n};\n\ntype Result<R> =\n | {\n result: R;\n error?: never;\n }\n | {\n result?: never;\n error: UnkeyError;\n };\n\nexport class Unkey {\n public readonly baseUrl: string;\n private readonly token: string;\n\n constructor(opts: UnkeyOptions) {\n this.baseUrl = opts.baseUrl ?? \"https://api.unkey.dev\";\n /**\n * Even though typescript should prevent this, some people still pass undefined or empty strings\n */\n if (!opts.token) {\n throw new Error(\"Unkey token must not be empty\");\n }\n this.token = opts.token;\n }\n\n private async fetch<TResult>(req: ApiRequest): Promise<Result<TResult>> {\n try {\n const url = `${this.baseUrl}/${req.path.join(\"/\")}`;\n const res = await fetch(url, {\n method: req.method,\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.token}`,\n },\n body: JSON.stringify(req.body),\n });\n if (!res.ok) {\n return { error: await res.json() };\n }\n return await res.json();\n } catch (e) {\n console.error(e);\n throw e;\n }\n }\n\n public get keys() {\n return {\n create: async (req: {\n /**\n * Provide a name to this key if you want for later reference\n */\n name?: string;\n /**\n * Choose an API where this key should be created.\n */\n apiId: string;\n /**\n * To make it easier for your users to understand which product an api key belongs to, you can add prefix them.\n *\n * For example Stripe famously prefixes their customer ids with cus_ or their api keys with sk_live_.\n *\n * The underscore is automtically added if you are defining a prefix, for example: \"prefix\": \"abc\" will result in a key like abc_xxxxxxxxx\n */\n prefix?: string;\n\n /**\n * The bytelength used to generate your key determines its entropy as well as its length. Higher is better, but keys become longer and more annoying to handle.\n *\n * The default is 16 bytes, or 2128 possible combinations\n */\n byteLength?: number;\n /**\n * Your user’s Id. This will provide a link between Unkey and your customer record.\n *\n * When validating a key, we will return this back to you, so you can clearly identify your user from their api key.\n */\n ownerId?: string;\n /**\n * This is a place for dynamic meta data, anything that feels useful for you should go here\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n meta?: unknown;\n /**\n * You can auto expire keys by providing a unix timestamp in milliseconds.\n *\n * Once keys expire they will automatically be deleted and are no longer valid.\n */\n expires?: number;\n\n /**\n * Unkey comes with per-key ratelimiting out of the box.\n *\n * @see https://docs.unkey.dev/features/ratelimiting\n */\n ratelimit?: {\n type: \"fast\" | \"consistent\";\n /**\n * The total amount of burstable requests.\n */\n limit: number;\n\n /**\n * How many tokens to refill during each refillInterval\n */\n refillRate: number;\n\n /**\n * Determines the speed at which tokens are refilled.\n * In milliseconds.\n */\n refillInterval: number;\n };\n\n /**\n * Unkey allows you to set/update usage limits on individual keys\n *\n * @see https://docs.unkey.dev/features/remaining\n */\n remaining?: number;\n }): Promise<Result<{ key: string; keyId: string }>> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"keys\"],\n method: \"POST\",\n body: req,\n });\n },\n update: async (req: {\n /**\n * The id of the key to update.\n */\n keyId: string;\n /**\n * Update the name\n */\n name?: string | null;\n\n /**\n * Update the owner id\n */\n ownerId?: string | null;\n /**\n * This is a place for dynamic meta data, anything that feels useful for you should go here\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n meta?: unknown | null;\n /**\n * Update the expiration time, Unix timstamp in milliseconds\n *\n *\n */\n expires?: number | null;\n\n /**\n * Update the ratelimit\n *\n * @see https://docs.unkey.dev/features/ratelimiting\n */\n ratelimit?: {\n type: \"fast\" | \"consistent\";\n /**\n * The total amount of burstable requests.\n */\n limit: number;\n\n /**\n * How many tokens to refill during each refillInterval\n */\n refillRate: number;\n\n /**\n * Determines the speed at which tokens are refilled.\n * In milliseconds.\n */\n refillInterval: number;\n } | null;\n\n /**\n * Update the remaining verifications.\n *\n * @see https://docs.unkey.dev/features/remaining\n */\n remaining?: number | null;\n }): Promise<Result<{ key: string; keyId: string }>> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"keys\", req.keyId],\n method: \"PUT\",\n body: req,\n });\n },\n verify: async (req: { key: string }): Promise<\n Result<{\n /**\n * Whether or not this key is valid and has passed the ratelimit. If false you should not grant access to whatever the user is requesting\n */\n valid: boolean;\n\n /**\n * If you have set an ownerId on this key it is returned here. You can use this to clearly authenticate a user in your system.\n */\n ownerId?: string;\n\n /**\n * This is the meta data you have set when creating the key.\n *\n * Example:\n *\n * ```json\n * {\n * \"billingTier\":\"PRO\",\n * \"trialEnds\": \"2023-06-16T17:16:37.161Z\"\n * }\n * ```\n */\n meta?: unknown;\n }>\n > => {\n return await this.fetch<{\n valid: boolean;\n ownerId?: string;\n meta?: unknown;\n }>({\n path: [\"v1\", \"keys\", \"verify\"],\n method: \"POST\",\n body: req,\n });\n },\n revoke: async (req: { keyId: string }): Promise<Result<void>> => {\n return await this.fetch<void>({\n path: [\"v1\", \"keys\", req.keyId],\n method: \"DELETE\",\n });\n },\n };\n }\n /**\n * Must be authenticated via app token\n */\n public get _internal() {\n return {\n createRootKey: async (req: {\n /**\n * Provide a name to this key if you want for later reference\n */\n name?: string;\n\n /**\n * You can auto expire keys by providing a unix timestamp in milliseconds.\n *\n * Once keys expire they will automatically be deleted and are no longer valid.\n */\n expires?: number;\n\n // Used to create root keys from the frontend, please ignore\n forWorkspaceId: string;\n }): Promise<Result<{ key: string; keyId: string }>> => {\n return await this.fetch<{ key: string; keyId: string }>({\n path: [\"v1\", \"internal\", \"rootkeys\"],\n method: \"POST\",\n body: req,\n });\n },\n };\n }\n}\n"],"mappings":";AA8BO,IAAM,QAAN,MAAY;AAAA,EACD;AAAA,EACC;AAAA,EAEjB,YAAY,MAAoB;AAC9B,SAAK,UAAU,KAAK,WAAW;AAI/B,QAAI,CAAC,KAAK,OAAO;AACf,YAAM,IAAI,MAAM,+BAA+B;AAAA,IACjD;AACA,SAAK,QAAQ,KAAK;AAAA,EACpB;AAAA,EAEA,MAAc,MAAe,KAA2C;AACtE,QAAI;AACF,YAAM,MAAM,GAAG,KAAK,OAAO,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC;AACjD,YAAM,MAAM,MAAM,MAAM,KAAK;AAAA,QAC3B,QAAQ,IAAI;AAAA,QACZ,SAAS;AAAA,UACP,gBAAgB;AAAA,UAChB,eAAe,UAAU,KAAK,KAAK;AAAA,QACrC;AAAA,QACA,MAAM,KAAK,UAAU,IAAI,IAAI;AAAA,MAC/B,CAAC;AACD,UAAI,CAAC,IAAI,IAAI;AACX,eAAO,EAAE,OAAO,MAAM,IAAI,KAAK,EAAE;AAAA,MACnC;AACA,aAAO,MAAM,IAAI,KAAK;AAAA,IACxB,SAAS,GAAG;AACV,cAAQ,MAAM,CAAC;AACf,YAAM;AAAA,IACR;AAAA,EACF;AAAA,EAEA,IAAW,OAAO;AAChB,WAAO;AAAA,MACL,QAAQ,OAAO,QAgFwC;AACrD,eAAO,MAAM,KAAK,MAAsC;AAAA,UACtD,MAAM,CAAC,MAAM,MAAM;AAAA,UACnB,QAAQ;AAAA,UACR,MAAM;AAAA,QACR,CAAC;AAAA,MACH;AAAA,MACA,QAAQ,OAAO,QAgEwC;AACrD,eAAO,MAAM,KAAK,MAAsC;AAAA,UACtD,MAAM,CAAC,MAAM,QAAQ,IAAI,KAAK;AAAA,UAC9B,QAAQ;AAAA,UACR,MAAM;AAAA,QACR,CAAC;AA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