Skip to content

Commit

Permalink
feat: override methods in sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Nov 18, 2024
1 parent e97c730 commit a92f1d7
Show file tree
Hide file tree
Showing 4 changed files with 649 additions and 669 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-spies-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@unkey/api": minor
---

Add ratelimit override API
40 changes: 20 additions & 20 deletions apps/api/src/routes/v1_keys_getVerifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ export const registerV1KeysGetVerifications = (app: App) =>
roles: dbRes.roles.map((p) => p.role.name),
identity: dbRes.identity
? {
id: dbRes.identity.id,
externalId: dbRes.identity.externalId,
meta: dbRes.identity.meta,
}
id: dbRes.identity.id,
externalId: dbRes.identity.externalId,
meta: dbRes.identity.meta,
}
: null,
};
});
Expand Down Expand Up @@ -249,27 +249,27 @@ export const registerV1KeysGetVerifications = (app: App) =>
verifications[d.time] = { success: 0, rateLimited: 0, usageExceeded: 0 };
}
switch (d.outcome) {
case "VALID":
// case "VALID":
verifications[d.time].success += d.count;
break;
break;
case "RATE_LIMITED":
verifications[d.time].rateLimited += d.count;
break;
verifications[d.time].rateLimited += d.count;
break;
case "USAGE_EXCEEDED":
verifications[d.time].usageExceeded += d.count;
break;
verifications[d.time].usageExceeded += d.count;
break;
}
}
}

return c.json({
verifications: Object.entries(verifications).map(
([time, { success, rateLimited, usageExceeded }]) => ({
time: Number.parseInt(time),
success,
rateLimited,
usageExceeded,
}),
),
});
return c.json({
verifications: Object.entries(verifications).map(
([time, { success, rateLimited, usageExceeded }]) => ({
time: Number.parseInt(time),
success,
rateLimited,
usageExceeded,
}),
),
});
});
164 changes: 82 additions & 82 deletions packages/api/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import { type Telemetry, getTelemetry } from "./telemetry";

export type UnkeyOptions = (
| {
token?: never;
token?: never;

/**
* The root key from unkey.dev.
*
* You can create/manage your root keys here:
* https://unkey.dev/app/settings/root-keys
*/
rootKey: string;
}
/**
* The root key from unkey.dev.
*
* You can create/manage your root keys here:
* https://unkey.dev/app/settings/root-keys
*/
rootKey: string;
}
| {
/**
* The workspace key from unkey.dev
*
* @deprecated Use `rootKey`
*/
token: string;
rootKey?: never;
}
/**
* The workspace key from unkey.dev
*
* @deprecated Use `rootKey`
*/
token: string;
rootKey?: never;
}
) & {
/**
* @default https://api.unkey.dev
Expand Down Expand Up @@ -77,32 +77,32 @@ export type UnkeyOptions = (
type ApiRequest = {
path: string[];
} & (
| {
| {
method: "GET";
body?: never;
query?: Record<string, string | number | boolean | null>;
}
| {
| {
method: "POST";
body?: unknown;
query?: never;
}
);
);

type Result<R> =
| {
result: R;
error?: never;
}
result: R;
error?: never;
}
| {
result?: never;
error: {
code: ErrorResponse["error"]["code"];
message: ErrorResponse["error"]["message"];
docs: ErrorResponse["error"]["docs"];
requestId: string;
};
result?: never;
error: {
code: ErrorResponse["error"]["code"];
message: ErrorResponse["error"]["message"];
docs: ErrorResponse["error"]["docs"];
requestId: string;
};
};

export class Unkey {
public readonly baseUrl: string;
Expand Down Expand Up @@ -376,60 +376,60 @@ export class Unkey {
body: req,
});
},
// getOverride: async (
// req: paths["/v1/ratelimits.getOverride"]["get"]["parameters"]["query"],
// ): Promise<
// Result<
// paths["/v1/ratelimits.getOverride"]["get"]["responses"]["200"]["content"]["application/json"]
// >
// > => {
// return await this.fetch({
// path: ["v1", "ratelimits.getOverride"],
// method: "GET",
// query: req,
// });
// },
// listOverrides: async (
// req: paths["/v1/ratelimits.listOverrides"]["get"]["parameters"]["query"],
// ): Promise<
// Result<
// paths["/v1/ratelimits.listOverrides"]["get"]["responses"]["200"]["content"]["application/json"]
// >
// > => {
// return await this.fetch({
// path: ["v1", "ratelimits.listOverrides"],
// method: "GET",
// query: req,
// });
// },
getOverride: async (
req: paths["/v1/ratelimits.getOverride"]["get"]["parameters"]["query"],
): Promise<
Result<
paths["/v1/ratelimits.getOverride"]["get"]["responses"]["200"]["content"]["application/json"]
>
> => {
return await this.fetch({
path: ["v1", "ratelimits.getOverride"],
method: "GET",
query: req,
});
},
listOverrides: async (
req: paths["/v1/ratelimits.listOverrides"]["get"]["parameters"]["query"],
): Promise<
Result<
paths["/v1/ratelimits.listOverrides"]["get"]["responses"]["200"]["content"]["application/json"]
>
> => {
return await this.fetch({
path: ["v1", "ratelimits.listOverrides"],
method: "GET",
query: req,
});
},

// setOverride: async (
// req: paths["/v1/ratelimits.setOverride"]["post"]["requestBody"]["content"]["application/json"],
// ): Promise<
// Result<
// paths["/v1/ratelimits.setOverride"]["post"]["responses"]["200"]["content"]["application/json"]
// >
// > => {
// return await this.fetch({
// path: ["v1", "ratelimits.setOverride"],
// method: "POST",
// body: req,
// });
// },
setOverride: async (
req: paths["/v1/ratelimits.setOverride"]["post"]["requestBody"]["content"]["application/json"],
): Promise<
Result<
paths["/v1/ratelimits.setOverride"]["post"]["responses"]["200"]["content"]["application/json"]
>
> => {
return await this.fetch({
path: ["v1", "ratelimits.setOverride"],
method: "POST",
body: req,
});
},

// deleteOverride: async (
// req: paths["/v1/ratelimits.deleteOverride"]["post"]["requestBody"]["content"]["application/json"],
// ): Promise<
// Result<
// paths["/v1/ratelimits.deleteOverride"]["post"]["responses"]["200"]["content"]["application/json"]
// >
// > => {
// return await this.fetch({
// path: ["v1", "ratelimits.deleteOverride"],
// method: "POST",
// body: req,
// });
// },
deleteOverride: async (
req: paths["/v1/ratelimits.deleteOverride"]["post"]["requestBody"]["content"]["application/json"],
): Promise<
Result<
paths["/v1/ratelimits.deleteOverride"]["post"]["responses"]["200"]["content"]["application/json"]
>
> => {
return await this.fetch({
path: ["v1", "ratelimits.deleteOverride"],
method: "POST",
body: req,
});
},
};
}
public get identities() {
Expand Down
Loading

0 comments on commit a92f1d7

Please sign in to comment.