Skip to content

Commit c7aad31

Browse files
Bug fixes for the ApiKeyProvider (#4)
When actually implementing the ApiKeyProvider, several type issues were discovered. This PR fixes: The api_key providers entrypoint is exported so it can be imported directly RuntError options are optional, and the empty {} can be omitted ListApiKeys takes in a limit/offset value ApiKeyProvider passes back an AuthenticatedContext, to prevent unecessary non-null assertions The index exports all the values from shared, not just the types
1 parent dce9435 commit c7aad31

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
".": {
2424
"import": "./dist/index.js",
2525
"types": "./dist/index.d.ts"
26+
},
27+
"./providers/api_key": {
28+
"import": "./dist/providers/api_key.js",
29+
"types": "./dist/providers/api_key.d.ts"
2630
}
2731
},
2832
"files": [

src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export type ErrorPayload = {
3535
export class RuntError extends Error {
3636
constructor(
3737
public type: ErrorType,
38-
private options: ExtensionErrorOptions
38+
private options: ExtensionErrorOptions = {}
3939
) {
4040
super(options.message ?? `RuntError: ${type}`, { cause: options.cause });
4141
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ApiKeyProvider } from './providers/api_key';
2-
export type * from './providers/shared';
2+
export * from './providers/shared';
33
export * from './errors';
44

55
export type BackendExtension = {

src/providers/api_key.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Response as WorkerResponse } from '@cloudflare/workers-types';
2-
import type { Passport, ProviderContext, Scope, Resource } from './shared';
2+
import type { Passport, ProviderContext, Scope, Resource, AuthenticatedProviderContext } from './shared';
33

44
export enum ApiKeyCapabilities {
55
Revoke = 'revoke',
@@ -21,14 +21,19 @@ export type ApiKey = CreateApiKeyRequest & {
2121
revoked: boolean;
2222
};
2323

24+
export type ListApiKeysRequest = {
25+
limit?: number;
26+
offset?: number;
27+
};
28+
2429
export type ApiKeyProvider = {
2530
capabilities: Set<ApiKeyCapabilities>;
2631
overrideHandler?: (context: ProviderContext) => Promise<false | WorkerResponse>; // Any routes the provider wants to fully implement. Return false for any route not handled
2732
isApiKey(context: ProviderContext): boolean; // Returns true if the auth token appears to be an API key (whether or not it is valid)
2833
validateApiKey(context: ProviderContext): Promise<Passport>; // Ensure the API key is valid, and returns the Passport. Raise an error otherwise
29-
createApiKey: (context: ProviderContext, request: CreateApiKeyRequest) => Promise<string>;
30-
getApiKey: (context: ProviderContext, id: string) => Promise<ApiKey>; // Returns the API key matching the specific api key id
31-
listApiKeys: (context: ProviderContext) => Promise<ApiKey[]>; // Return a list of all API keys for a user
32-
revokeApiKey: (context: ProviderContext, id: string) => Promise<void>; // set the revoked flag for an API key, such that it will no longer be valid
33-
deleteApiKey: (context: ProviderContext, id: string) => Promise<void>; // Delete an API key from the database
34+
createApiKey: (context: AuthenticatedProviderContext, request: CreateApiKeyRequest) => Promise<string>;
35+
getApiKey: (context: AuthenticatedProviderContext, id: string) => Promise<ApiKey>; // Returns the API key matching the specific api key id
36+
listApiKeys: (context: AuthenticatedProviderContext, request: ListApiKeysRequest) => Promise<ApiKey[]>; // Return a list of all API keys for a user
37+
revokeApiKey: (context: AuthenticatedProviderContext, id: string) => Promise<void>; // set the revoked flag for an API key, such that it will no longer be valid
38+
deleteApiKey: (context: AuthenticatedProviderContext, id: string) => Promise<void>; // Delete an API key from the database
3439
};

src/providers/shared.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type User = {
2323
email: string;
2424
name?: string;
2525
givenName?: string;
26-
familyName: string;
26+
familyName?: string;
2727
};
2828

2929
export type Resource = {
@@ -46,3 +46,9 @@ export type ProviderContext = {
4646
bearerToken: string | null;
4747
passport: Passport | null;
4848
};
49+
50+
// https://github.com/microsoft/TypeScript/issues/28374
51+
type NonNullableValues<T> = {
52+
[P in keyof T]-?: NonNullable<T[P]>;
53+
};
54+
export type AuthenticatedProviderContext = NonNullableValues<ProviderContext>;

0 commit comments

Comments
 (0)