Skip to content

Commit

Permalink
feat: retire name parameter of token related endpoints (#1582)
Browse files Browse the repository at this point in the history
Because

- retire name parameter of token related endpoints

This commit

- retire name parameter of token related endpoints
  • Loading branch information
EiffelFly authored Nov 3, 2024
1 parent 4b9c21a commit 0de03b3
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 205 deletions.
135 changes: 80 additions & 55 deletions packages/sdk/src/core/token/TokenClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { getQueryString } from "../../helper";
import { APIResource } from "../../main/resource";
import {
ApiToken,
CreateApiTokenRequest,
CreateApiTokenResponse,
DeleteApiTokenRequest,
GetApiTokenRequest,
GetApiTokenResponse,
ListApiTokenRequest,
ListApiTokensResponse,
APIToken,
CreateAPITokenRequest,
CreateAPITokenResponse,
DeleteAPITokenRequest,
GetAPITokenRequest,
GetAPITokenResponse,
ListAPITokenRequest,
ListAPITokensResponse,
ListPaginatedAPITokenRequest,
ListPaginatedAPITokensResponse,
} from "./type";

export class TokenClient extends APIResource {
Expand All @@ -19,51 +21,59 @@ export class TokenClient extends APIResource {
/**
* Returns a paginated list of the API tokens of the authenticated user.
*/
async listAPITokens(
props: ListApiTokenRequest & { enablePagination: true },
): Promise<ListApiTokensResponse>;
async listAPITokens(
props: ListApiTokenRequest & { enablePagination: false },
): Promise<ApiToken[]>;
async listAPITokens(
props: ListApiTokenRequest & { enablePagination: undefined },
): Promise<ApiToken[]>;
async listAPITokens(
props: ListApiTokenRequest & { enablePagination?: boolean },
): Promise<ListApiTokensResponse | ApiToken[]>;
async listAPITokens(
props: ListApiTokenRequest & { enablePagination?: boolean },
) {
const { pageSize, pageToken, enablePagination } = props;
async listPaginatedAPITokens({
pageSize,
pageToken,
}: ListPaginatedAPITokenRequest) {
const queryString = getQueryString({
baseURL: "/tokens",
pageSize,
pageToken,
});

try {
const tokens: ApiToken[] = [];
const data =
await this._client.get<ListPaginatedAPITokensResponse>(queryString);
return Promise.resolve(data);
} catch (error) {
return Promise.reject(error);
}
}

const queryString = getQueryString({
baseURL: "/tokens",
pageSize,
pageToken,
});
/**
* Returns a list of the API tokens of the authenticated user.
*/
async listAPITokens({ pageSize = 100, pageToken }: ListAPITokenRequest) {
const queryString = getQueryString({
baseURL: "/tokens",
pageSize,
pageToken,
});

const data = await this._client.get<ListApiTokensResponse>(queryString);
try {
const tokens: APIToken[] = [];

if (enablePagination) {
return Promise.resolve(data);
}
const res =
await this._client.get<ListPaginatedAPITokensResponse>(queryString);

tokens.push(...data.tokens);
tokens.push(...res.tokens);

if (data.nextPageToken) {
if (res.nextPageToken) {
tokens.push(
...(await this.listAPITokens({
pageSize,
pageToken: data.nextPageToken,
enablePagination: false,
})),
...(
await this.listAPITokens({
pageSize,
pageToken: res.nextPageToken,
})
).tokens,
);
}

return Promise.resolve(tokens);
const response: ListAPITokensResponse = {
tokens,
};

return Promise.resolve(response);
} catch (error) {
return Promise.reject(error);
}
Expand All @@ -72,10 +82,14 @@ export class TokenClient extends APIResource {
/**
* Returns the details of an API token.
*/
async getApiToken({ tokenName }: GetApiTokenRequest) {
async getAPIToken({ tokenId }: GetAPITokenRequest) {
const queryString = getQueryString({
baseURL: `/tokens/${tokenId}`,
});

try {
const data = await this._client.get<GetApiTokenResponse>(`/${tokenName}`);
return Promise.resolve(data.token);
const data = await this._client.get<GetAPITokenResponse>(queryString);
return Promise.resolve(data);
} catch (error) {
return Promise.reject(error);
}
Expand All @@ -88,15 +102,22 @@ export class TokenClient extends APIResource {
/**
* Creates an API token for the authenticated user.
*/
async createApiToken({ id, ttl }: CreateApiTokenRequest) {
async createAPIToken({ id, ttl }: CreateAPITokenRequest) {
const queryString = getQueryString({
baseURL: "/tokens",
});

try {
const data = await this._client.post<CreateApiTokenResponse>("/tokens", {
body: JSON.stringify({
id,
ttl,
}),
});
return Promise.resolve(data.token);
const data = await this._client.post<CreateAPITokenResponse>(
queryString,
{
body: JSON.stringify({
id,
ttl,
}),
},
);
return Promise.resolve(data);
} catch (error) {
return Promise.reject(error);
}
Expand All @@ -105,9 +126,13 @@ export class TokenClient extends APIResource {
/**
* Deletes an API token.
*/
async deleteApiToken({ tokenName }: DeleteApiTokenRequest) {
async deleteAPIToken({ tokenId }: DeleteAPITokenRequest) {
const queryString = getQueryString({
baseURL: `/tokens/${tokenId}`,
});

try {
await this._client.delete(`/${tokenName}`);
await this._client.delete(queryString);
} catch (error) {
return Promise.reject(error);
}
Expand Down
43 changes: 22 additions & 21 deletions packages/sdk/src/core/token/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ApiToken = {
export type APIToken = {
name: string;
uid: string;
id: string;
Expand All @@ -16,42 +16,43 @@ export type ApiTokenState =
| "STATE_ACTIVE"
| "STATE_EXPIRED";

export type ListApiTokenRequest = {
export type ListPaginatedAPITokenRequest = {
pageSize?: number;
pageToken?: string;
};

export type ListApiTokensResponse = {
tokens: ApiToken[];
export type ListPaginatedAPITokensResponse = {
tokens: APIToken[];
nextPageToken: string;
totalSize: string;
};

export type GetApiTokenRequest = {
/**
* The resource name of the token, which allows its access by ID.
* Format: tokens/{token.id}.
*/
tokenName: string;
export type ListAPITokenRequest = {
pageSize?: number;
pageToken?: string;
};

export type ListAPITokensResponse = {
tokens: APIToken[];
};

export type GetAPITokenRequest = {
tokenId: string;
};

export type GetApiTokenResponse = {
token: ApiToken;
export type GetAPITokenResponse = {
token: APIToken;
};

export type CreateApiTokenRequest = {
export type CreateAPITokenRequest = {
id: string;
ttl: number;
};

export type CreateApiTokenResponse = {
token: ApiToken;
export type CreateAPITokenResponse = {
token: APIToken;
};

export type DeleteApiTokenRequest = {
/**
* The resource name of the token, which allows its access by ID.
* Format: tokens/{token.id}.
*/
tokenName: string;
export type DeleteAPITokenRequest = {
tokenId: string;
};
6 changes: 3 additions & 3 deletions packages/toolkit/src/lib/react-query-service/mgmt/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export { useApiToken } from "./useApiToken";
export { useAuthenticatedUserSubscription } from "./useAuthenticatedUserSubscription";
export { useCreateApiToken } from "./useCreateApiToken";
export { useDeleteApiToken } from "./useDeleteApiToken";
export { useMgmtDefinition } from "./useMgmtDefinition";
export { useNamespaceType } from "./use-namespace-type/client";

Expand All @@ -13,6 +10,9 @@ export * from "./use-namespace-type";
export * from "./use-user";
export * from "./use-api-tokens";

export * from "./useAPIToken";
export * from "./useCreateAPIToken";
export * from "./useDeleteAPIToken";
export * from "./onTriggerInvalidateCredits";
export * from "./useGetNamespaceRemainingInstillCredit";
export * from "./useListNamespacesRemainingInstillCredit";
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
"use client";

import type { Nullable } from "instill-sdk";
import { useQuery } from "@tanstack/react-query";

import { getInstillAPIClient } from "../../../sdk-helper";
import { Nullable } from "../../../type";
import { getUseApiTokensQueryKey } from "./server";
import { queryKeyStore } from "../../queryKeyStore";
import { fetchAPITokens } from "./server";

export function useApiTokens({
export function useAPITokens({
accessToken,
enabled,
}: {
accessToken: Nullable<string>;
enabled: boolean;
}) {
const queryKey = getUseApiTokensQueryKey();
return useQuery({
queryKey,
queryKey: queryKeyStore.mgmt.getUseAPITokensQueryKey(),
queryFn: async () => {
if (!accessToken) {
return Promise.reject(new Error("accessToken not provided"));
}

const client = getInstillAPIClient({ accessToken });

const tokens = await client.core.token.listAPITokens({
enablePagination: false,
return await fetchAPITokens({
accessToken,
});

return Promise.resolve(tokens);
},
enabled,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,41 @@ import { QueryClient } from "@tanstack/react-query";
import { env } from "../../../../server";
import { getInstillAPIClient } from "../../../sdk-helper";
import { Nullable } from "../../../type";
import { queryKeyStore } from "../../queryKeyStore";

export async function fetchApiTokens({
export async function fetchAPITokens({
accessToken,
}: {
accessToken: Nullable<string>;
}) {
if (!accessToken) {
return Promise.reject(new Error("accessToken not provided"));
return Promise.reject(new Error("accessToken is required"));
}

try {
const client = getInstillAPIClient({ accessToken });

const apiTokens = await client.core.token.listAPITokens({
const res = await client.core.token.listAPITokens({
pageSize: env("NEXT_PUBLIC_QUERY_PAGE_SIZE"),
enablePagination: false,
});

return Promise.resolve(apiTokens);
return Promise.resolve(res.tokens);
} catch (error) {
return Promise.reject(error);
}
}

export function getUseApiTokensQueryKey() {
return ["api-tokens"];
}

export function prefetchApiTokens({
export function prefetchAPITokens({
accessToken,
queryClient,
}: {
accessToken: Nullable<string>;
queryClient: QueryClient;
}) {
const queryKey = getUseApiTokensQueryKey();

return queryClient.prefetchQuery({
queryKey,
queryKey: queryKeyStore.mgmt.getUseAPITokensQueryKey(),
queryFn: async () => {
return await fetchApiTokens({
return await fetchAPITokens({
accessToken,
});
},
Expand Down
Loading

0 comments on commit 0de03b3

Please sign in to comment.