@unkey/api 0.4.0 → 0.5.0-canary.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,199 @@
1
+ type UnkeyOptions = {
2
+ /**
3
+ * @default https://api.unkey.dev
4
+ */
5
+ baseUrl?: string;
6
+ /**
7
+ * The workspace key from unkey.dev
8
+ */
9
+ token: string;
10
+ };
11
+ declare class Unkey {
12
+ readonly baseUrl: string;
13
+ private readonly token;
14
+ constructor(opts: UnkeyOptions);
15
+ private fetch;
16
+ get keys(): {
17
+ create: (req: {
18
+ /**
19
+ * Provide a name to this key if you want for later reference
20
+ */
21
+ name?: string;
22
+ /**
23
+ * Choose an API where this key should be created.
24
+ */
25
+ apiId: string;
26
+ /**
27
+ * To make it easier for your users to understand which product an api key belongs to, you can add prefix them.
28
+ *
29
+ * For example Stripe famously prefixes their customer ids with cus_ or their api keys with sk_live_.
30
+ *
31
+ * The underscore is automtically added if you are defining a prefix, for example: "prefix": "abc" will result in a key like abc_xxxxxxxxx
32
+ */
33
+ prefix?: string;
34
+ /**
35
+ * 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.
36
+ *
37
+ * The default is 16 bytes, or 2128 possible combinations
38
+ */
39
+ byteLength?: number;
40
+ /**
41
+ * Your user’s Id. This will provide a link between Unkey and your customer record.
42
+ *
43
+ * When validating a key, we will return this back to you, so you can clearly identify your user from their api key.
44
+ */
45
+ ownerId?: string;
46
+ /**
47
+ * This is a place for dynamic meta data, anything that feels useful for you should go here
48
+ *
49
+ * Example:
50
+ *
51
+ * ```json
52
+ * {
53
+ * "billingTier":"PRO",
54
+ * "trialEnds": "2023-06-16T17:16:37.161Z"
55
+ * }
56
+ * ```
57
+ */
58
+ meta?: unknown;
59
+ /**
60
+ * You can auto expire keys by providing a unix timestamp in milliseconds.
61
+ *
62
+ * Once keys expire they will automatically be deleted and are no longer valid.
63
+ */
64
+ expires?: number;
65
+ /**
66
+ * Unkey comes with per-key ratelimiting out of the box.
67
+ *
68
+ * @see https://docs.unkey.dev/features/ratelimiting
69
+ */
70
+ ratelimit?: {
71
+ type: "fast" | "consistent";
72
+ /**
73
+ * The total amount of burstable requests.
74
+ */
75
+ limit: number;
76
+ /**
77
+ * How many tokens to refill during each refillInterval
78
+ */
79
+ refillRate: number;
80
+ /**
81
+ * Determines the speed at which tokens are refilled.
82
+ * In milliseconds.
83
+ */
84
+ refillInterval: number;
85
+ };
86
+ /**
87
+ * Unkey allows you to set/update usage limits on individual keys
88
+ *
89
+ * @see https://docs.unkey.dev/features/remaining
90
+ */
91
+ remaining?: number;
92
+ }) => Promise<{
93
+ key: string;
94
+ keyId: string;
95
+ }>;
96
+ update: (req: {
97
+ /**
98
+ * The id of the key to update.
99
+ */
100
+ keyId: string;
101
+ /**
102
+ * Update the name
103
+ */
104
+ name?: string | null;
105
+ /**
106
+ * Update the owner id
107
+ */
108
+ ownerId?: string | null;
109
+ /**
110
+ * This is a place for dynamic meta data, anything that feels useful for you should go here
111
+ *
112
+ * Example:
113
+ *
114
+ * ```json
115
+ * {
116
+ * "billingTier":"PRO",
117
+ * "trialEnds": "2023-06-16T17:16:37.161Z"
118
+ * }
119
+ * ```
120
+ */
121
+ meta?: unknown | null;
122
+ /**
123
+ * Update the expiration time, Unix timstamp in milliseconds
124
+ *
125
+ *
126
+ */
127
+ expires?: number | null;
128
+ /**
129
+ * Update the ratelimit
130
+ *
131
+ * @see https://docs.unkey.dev/features/ratelimiting
132
+ */
133
+ ratelimit?: {
134
+ type: "fast" | "consistent";
135
+ /**
136
+ * The total amount of burstable requests.
137
+ */
138
+ limit: number;
139
+ /**
140
+ * How many tokens to refill during each refillInterval
141
+ */
142
+ refillRate: number;
143
+ /**
144
+ * Determines the speed at which tokens are refilled.
145
+ * In milliseconds.
146
+ */
147
+ refillInterval: number;
148
+ } | null;
149
+ /**
150
+ * Update the remaining verifications.
151
+ *
152
+ * @see https://docs.unkey.dev/features/remaining
153
+ */
154
+ remaining?: number | null;
155
+ }) => Promise<{
156
+ key: string;
157
+ keyId: string;
158
+ }>;
159
+ verify: (req: {
160
+ key: string;
161
+ }) => Promise<{
162
+ /**
163
+ * 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
+ */
165
+ valid: boolean;
166
+ /**
167
+ * 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
+ */
169
+ ownerId?: string;
170
+ meta?: unknown;
171
+ }>;
172
+ revoke: (req: {
173
+ keyId: string;
174
+ }) => Promise<void>;
175
+ };
176
+ /**
177
+ * Must be authenticated via app token
178
+ */
179
+ get _internal(): {
180
+ createRootKey: (req: {
181
+ /**
182
+ * Provide a name to this key if you want for later reference
183
+ */
184
+ name?: string;
185
+ /**
186
+ * You can auto expire keys by providing a unix timestamp in milliseconds.
187
+ *
188
+ * Once keys expire they will automatically be deleted and are no longer valid.
189
+ */
190
+ expires?: number;
191
+ forWorkspaceId: string;
192
+ }) => Promise<{
193
+ key: string;
194
+ keyId: string;
195
+ }>;
196
+ };
197
+ }
198
+
199
+ export { Unkey, UnkeyOptions };
package/dist/index.js CHANGED
@@ -35,7 +35,6 @@ var Unkey = class {
35
35
  }
36
36
  async fetch(req) {
37
37
  const url = `${this.baseUrl}/${req.path.join("/")}`;
38
- console.log("fetching", url);
39
38
  const res = await fetch(url, {
40
39
  method: req.method,
41
40
  headers: {
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 console.log(\"fetching\", url);\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,YAAQ,IAAI,YAAY,GAAG;AAC3B,UAAM,MAAM,MAAM,MAAM,KAAK;AAAA,MAC3B,QAAQ,IAAI;AAAA,MACZ,SAAS;AAAA,QACP,gBAAgB;AAAA,QAChB,eAAe,UAAU,KAAK,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keyOptions = {\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;AAA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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":[]}
package/dist/index.mjs CHANGED
@@ -11,7 +11,6 @@ var Unkey = class {
11
11
  }
12
12
  async fetch(req) {
13
13
  const url = `${this.baseUrl}/${req.path.join("/")}`;
14
- console.log("fetching", url);
15
14
  const res = await fetch(url, {
16
15
  method: req.method,
17
16
  headers: {
@@ -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 console.log(\"fetching\", url);\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;AACA,SAAK,QAAQ,KAAK;AAAA,EACpB;AAAA,EAEA,MAAc,MAAe,KAAmC;AAC9D,UAAM,MAAM,GAAG,KAAK,OAAO,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC;AACjD,YAAQ,IAAI,YAAY,GAAG;AAC3B,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":["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;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":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unkey/api",
3
- "version": "0.4.0",
3
+ "version": "0.5.0-canary.1",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "license": "MIT",
@@ -19,10 +19,10 @@
19
19
  ],
20
20
  "author": "Andreas Thomas <andreas@chronark.com>",
21
21
  "devDependencies": {
22
- "@types/node": "^20.2.1",
23
- "tsup": "^7.0.0",
22
+ "@types/node": "^20.4.4",
23
+ "tsup": "^7.1.0",
24
24
  "tsx": "^3.12.7",
25
- "typescript": "^5.0.4"
25
+ "typescript": "^5.1.6"
26
26
  },
27
27
  "scripts": {
28
28
  "build": "tsup"