Skip to content

Commit f27c87c

Browse files
authored
Merge pull request #1 from lukas-reineke/export-api-types
export api types
2 parents dc85963 + 53938df commit f27c87c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ export interface Error {
1313
code?: codes;
1414
};
1515
}
16+
export declare type APIKey = keyof (Query & Mutation);
17+
export declare type APIResult<T extends APIKey, U extends string = T> = {
18+
data: Record<U, NonNullable<Partial<(Query & Mutation)[T]>>>;
19+
};
1620
export declare class Cinnamon {
1721
config: Config;
1822
refreshToken: string;
1923
token: string;
2024
constructor(config: Config);
21-
api<T extends keyof (Query & Mutation), U extends string = T>(query: string, variables?: object, headers?: Headers, token?: string): Promise<{
22-
data: Record<U, NonNullable<Partial<(Query & Mutation)[T]>>>;
23-
}>;
25+
api<T extends APIKey, U extends string = T>(query: string, variables?: object, headers?: Headers, token?: string): Promise<APIResult<T, U>>;
2426
apiPaging<T>(query: string, variables: object | undefined, headers: Headers, token?: string): Promise<Partial<T>[]>;
2527
login(input: UserLoginInput): Promise<Partial<import("./generated/graphql").Token>>;
2628
refreshLogin(input: RefreshTokenInput): Promise<Partial<import("./generated/graphql").Token>>;

src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export interface Error {
5858
extentions?: { code?: codes };
5959
}
6060

61+
export type APIKey = keyof (Query & Mutation);
62+
export type APIResult<T extends APIKey, U extends string = T> = {
63+
data: Record<U, NonNullable<Partial<(Query & Mutation)[T]>>>;
64+
};
65+
6166
export class Cinnamon {
6267
config: Config;
6368
refreshToken = '';
@@ -67,14 +72,12 @@ export class Cinnamon {
6772
this.config = config;
6873
}
6974

70-
async api<T extends keyof (Query & Mutation), U extends string = T>(
75+
async api<T extends APIKey, U extends string = T>(
7176
query: string,
7277
variables: object = {},
7378
headers: Headers = {},
7479
token?: string,
75-
): Promise<{
76-
data: Record<U, NonNullable<Partial<(Query & Mutation)[T]>>>;
77-
}> {
80+
): Promise<APIResult<T, U>> {
7881
const response = await fetch(this.config.url, {
7982
method: 'POST',
8083
headers: {

0 commit comments

Comments
 (0)