Skip to content

Commit

Permalink
feat: disable-caching and list keys
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Sep 15, 2023
1 parent 952dcc2 commit 274bc31
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unkey/api",
"version": "0.1.0",
"version": "dev",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"license": "MIT",
Expand Down
95 changes: 94 additions & 1 deletion packages/sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type ApiRequest = {
path: string[];
method: "GET" | "POST" | "PUT" | "DELETE";
body?: unknown;
query?: Record<string, string>;
cache?: RequestCache;
};

type Result<R> =
Expand Down Expand Up @@ -76,7 +78,12 @@ export class Unkey {
let res: Response | null = null;
let err: Error | null = null;
for (let i = 0; i <= this.retry.attempts; i++) {
const url = `${this.baseUrl}/${req.path.join("/")}`;
const url = new URL(`${this.baseUrl}/${req.path.join("/")}`);
if (req.query) {
for (const [k, v] of Object.entries(req.query)) {
url.searchParams.set(k, v);
}
}
res = await fetch(url, {
method: req.method,
headers: {
Expand Down Expand Up @@ -206,6 +213,7 @@ export class Unkey {
path: ["v1", "keys"],
method: "POST",
body: req,
cache: "no-cache",
});
},
update: async (req: {
Expand Down Expand Up @@ -277,6 +285,7 @@ export class Unkey {
path: ["v1", "keys", req.keyId],
method: "PUT",
body: req,
cache: "no-cache",
});
},
verify: async (req: { key: string }): Promise<
Expand Down Expand Up @@ -350,12 +359,95 @@ export class Unkey {
path: ["v1", "keys", "verify"],
method: "POST",
body: req,
cache: "no-cache",
});
},
revoke: async (req: { keyId: string }): Promise<Result<void>> => {
return await this.fetch<void>({
path: ["v1", "keys", req.keyId],
method: "DELETE",
cache: "no-cache",
});
},
};
}

public get apis() {
return {
get: async (req: {
/**
* The api id
*/
apiId: string;
}): Promise<Result<{ id: string; name: string; workspaceId: string }>> => {
return await this.fetch({
path: ["v1", "apis", req.apiId],
method: "GET",
cache: "no-cache",
});
},

listKeys: async (req: {
/**
* The api id
*/
apiId: string;

/**
* Limit the number of returned keys, the maximum is 100.
*
* @default 100
*/
limit?: number;

/**
* Specify an offset for pagination.
* @example:
* An offset of 4 will skip the first 4 keys and return keys starting at the 5th position.
*
* @default 0
*/
offset?: number;

/**
* If provided, this will only return keys where the ownerId matches.
*/
ownerId?: string;
}): Promise<
Result<{
keys: {
id: string;
apiId: string;
workspaceId: string;
start: string;
createdAt: number;
expires: number | null;
ratelimit?: {
type: "fast" | "consistent";
limit: number;
refillRate: number;
refillInterval: number;
};
}[];
total: number;
}>
> => {
const query: Record<string, string> = {};
if (typeof req.limit !== "undefined") {
query.limit = req.limit.toString();
}
if (typeof req.offset !== "undefined") {
query.offset = req.offset.toString();
}
if (typeof req.ownerId !== "undefined") {
query.ownerId = req.ownerId;
}

return await this.fetch({
path: ["v1", "apis", req.apiId, "keys"],
method: "GET",
query,
cache: "no-cache",
});
},
};
Expand Down Expand Up @@ -385,6 +477,7 @@ export class Unkey {
path: ["v1", "internal", "rootkeys"],
method: "POST",
body: req,
cache: "no-cache",
});
},
};
Expand Down

1 comment on commit 274bc31

@vercel
Copy link

@vercel vercel bot commented on 274bc31 Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

unkey – ./

unkey-unkey.vercel.app
unkey.vercel.app
unkey-git-main-unkey.vercel.app
www.unkey.dev
unkey.dev

Please sign in to comment.