Skip to content

Commit

Permalink
feat: add conditional types to createKindeServerClient to select the …
Browse files Browse the repository at this point in the history
…type of options and client
  • Loading branch information
coel committed Sep 14, 2023
1 parent fec06d5 commit 28a3695
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ import {
createKindeServerClient,
GrantType,
type ACClientOptions,
type ACClient,
} from "@kinde-oss/kinde-typescript-sdk";

const clientOptions: ACClientOptions = {
Expand All @@ -90,7 +89,7 @@ const clientOptions: ACClientOptions = {
redirectURL: process.env.KINDE_REDIRECT_URL
};

const client = createKindeServerClient<ACClient, ACClientOptions>(
const client = createKindeServerClient(
GrantType.AUTHORIZATION_CODE, clientOptions
);
```
Expand All @@ -101,7 +100,6 @@ import {
createKindeServerClient,
GrantType,
type PKCEClientOptions,
type ACClient,
} from "@kinde-oss/kinde-typescript-sdk";

const clientOptions: PKCEClientOptions = {
Expand All @@ -111,7 +109,7 @@ const clientOptions: PKCEClientOptions = {
redirectURL: process.env.KINDE_REDIRECT_URL
};

const client = createKindeServerClient<ACClient, PKCEClientOptions>(
const client = createKindeServerClient(
GrantType.PKCE, clientOptions
);
```
Expand All @@ -122,7 +120,6 @@ import {
createKindeServerClient,
GrantType,
type CCClientOptions,
type CCClient,
} from "@kinde-oss/kinde-typescript-sdk";

const clientOptions: CCClientOptions = {
Expand All @@ -132,7 +129,7 @@ const clientOptions: CCClientOptions = {
logoutRedirectURL: process.env.KINDE_LOGOUT_REDIRECT_URL
};

const client = createKindeServerClient<CCClient, CCClientOptions>(
const client = createKindeServerClient(
GrantType.CLIENT_CREDENTIALS, clientOptions
)
```
Expand Down Expand Up @@ -181,7 +178,6 @@ import {
createKindeServerClient,
GrantType,
type ACClientOptions,
type ACClient,
} from "@kinde-oss/kinde-typescript-sdk";

const clientOptions: ACClientOptions = {
Expand All @@ -194,7 +190,7 @@ const clientOptions: ACClientOptions = {
audience: 'api.example.com/v1'
};

const client = createKindeServerClient<ACClient, ACClientOptions>(
const client = createKindeServerClient(
GrantType.AUTHORIZATION_CODE, clientOptions
);
```
Expand Down Expand Up @@ -230,7 +226,6 @@ import {
createKindeServerClient,
GrantType,
type ACClientOptions,
type ACClient,
} from "@kinde-oss/kinde-typescript-sdk";

const clientOptions: ACClientOptions = {
Expand All @@ -243,7 +238,7 @@ const clientOptions: ACClientOptions = {
framework: 'ExpressJS'
};

const client = createKindeServerClient<ACClient, ACClientOptions>(
const client = createKindeServerClient(
GrantType.AUTHORIZATION_CODE, clientOptions
);
```
Expand Down
7 changes: 1 addition & 6 deletions lib/apis/RolesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export interface DeleteRoleRequest {

export interface GetRolePermissionRequest {
roleId: string;
permissionId: string;
sort?: GetRolePermissionSortEnum;
pageSize?: number | null;
nextToken?: string | null;
Expand Down Expand Up @@ -173,10 +172,6 @@ export class RolesApi extends runtime.BaseAPI {
throw new runtime.RequiredError('roleId','Required parameter requestParameters.roleId was null or undefined when calling getRolePermission.');
}

if (requestParameters.permissionId === null || requestParameters.permissionId === undefined) {
throw new runtime.RequiredError('permissionId','Required parameter requestParameters.permissionId was null or undefined when calling getRolePermission.');
}

const queryParameters: any = {};

if (requestParameters.sort !== undefined) {
Expand All @@ -202,7 +197,7 @@ export class RolesApi extends runtime.BaseAPI {
}
}
const response = await this.request({
path: `/api/v1/roles/{role_id}/permission/{permission_id}`.replace(`{${"role_id"}}`, encodeURIComponent(String(requestParameters.roleId))).replace(`{${"permission_id"}}`, encodeURIComponent(String(requestParameters.permissionId))),
path: `/api/v1/roles/{role_id}/permissions`.replace(`{${"role_id"}}`, encodeURIComponent(String(requestParameters.roleId))),
method: 'GET',
headers: headerParameters,
query: queryParameters,
Expand Down
18 changes: 9 additions & 9 deletions lib/sdk/clients/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import type {
CCClientOptions,
} from '../types';

export const createKindeServerClient = <
C extends ACClient | CCClient,
O extends ACClientOptions | PKCEClientOptions | CCClientOptions
>(
grantType: GrantType,
options: O
type Options<T> = T extends GrantType.PKCE ? PKCEClientOptions : T extends GrantType.AUTHORIZATION_CODE ? ACClientOptions : T extends GrantType.CLIENT_CREDENTIALS ? CCClientOptions : never;
type Client<T> = T extends PKCEClientOptions ? ACClient : T extends ACClientOptions ? ACClient : T extends CCClientOptions ? CCClient : never;

export const createKindeServerClient = <G extends GrantType>(
grantType: G,
options: Options<G>
) => {
if (!isNodeEnvironment()) {
throw new Error('this method must be invoked in a node.js environment');
Expand All @@ -25,15 +25,15 @@ export const createKindeServerClient = <
switch (grantType) {
case GrantType.AUTHORIZATION_CODE: {
const clientOptions = options as ACClientOptions;
return createAuthCodeClient(clientOptions, false) as C;
return createAuthCodeClient(clientOptions, false) as Client<Options<G>>;
}
case GrantType.PKCE: {
const clientOptions = options as PKCEClientOptions;
return createAuthCodeClient(clientOptions, true) as C;
return createAuthCodeClient(clientOptions, true) as Client<Options<G>>;
}
case GrantType.CLIENT_CREDENTIALS: {
const clientOptions = options as CCClientOptions;
return createCCClient(clientOptions) as C;
return createCCClient(clientOptions) as Client<Options<G>>;
}
default: {
throw new Error('Unrecognized grant type provided');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kinde-oss/kinde-typescript-sdk",
"version": "2.0.0",
"version": "2.1.0",
"description": "Kinde Typescript SDK",
"main": "dist-cjs/index.js",
"module": "dist/index.js",
Expand Down

0 comments on commit 28a3695

Please sign in to comment.