Skip to content

Commit 72b4b87

Browse files
authored
chore: add types to authtypes (#241)
1 parent e7a1bec commit 72b4b87

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vesselapi/integrations",
3-
"version": "1.0.31",
3+
"version": "1.0.32",
44
"description": "Vessel integrations",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

src/sdk/types.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,28 @@ export type BasicMetadata = {
2222
answers: Record<string, string>;
2323
};
2424

25+
export type AuthMetadata = OAuth2Metadata | ApiKeyMetadata | BasicMetadata;
26+
export type AuthType = AuthMetadata['type'];
27+
2528
type BaseFetchResult = {
2629
status: number;
2730
text: () => string;
2831
json: () => Json;
2932
response: Response | unknown;
3033
};
3134

32-
type BaseAuth = {
35+
type BaseAuth<T extends AuthMetadata> = {
36+
type: T['type'];
37+
getMetadata: () => Promise<T>;
3338
getToken: () => Promise<string>;
3439
retry: <TResult extends BaseFetchResult>(
3540
func: () => Promise<TResult>,
3641
) => Promise<TResult>;
3742
};
3843

39-
export type OAuth2Auth = BaseAuth & {
40-
type: 'oauth2';
41-
getMetadata: () => Promise<OAuth2Metadata>;
42-
};
43-
44-
export type ApiKeyAuth = BaseAuth & {
45-
type: 'apiKey';
46-
getMetadata: () => Promise<ApiKeyMetadata>;
47-
};
48-
49-
export type BasicAuth = BaseAuth & {
50-
type: 'basic';
51-
getMetadata: () => Promise<BasicMetadata>;
52-
};
44+
export type OAuth2Auth = BaseAuth<OAuth2Metadata>;
45+
export type ApiKeyAuth = BaseAuth<ApiKeyMetadata>;
46+
export type BasicAuth = BaseAuth<BasicMetadata>;
5347

5448
export type Auth = OAuth2Auth | ApiKeyAuth | BasicAuth;
5549

@@ -99,11 +93,14 @@ export type StandardAuthConfig<
9993
toTokenString: (answers: TAnswers) => string;
10094
};
10195

96+
type BaseConfig<T extends AuthMetadata> = {
97+
type: T['type'];
98+
default: boolean;
99+
};
100+
102101
export type ApiKeyAuthConfig<
103102
TAnswers extends Record<string, string> = Record<string, string>,
104-
> = {
105-
type: 'apiKey';
106-
default: boolean;
103+
> = BaseConfig<ApiKeyMetadata> & {
107104
/**
108105
* Used by the FE to render form fields.
109106
* E.g. Asking for Api token
@@ -117,9 +114,7 @@ export type ApiKeyAuthConfig<
117114

118115
export type BasicAuthConfig<
119116
TAnswers extends Record<string, string> = Record<string, string>,
120-
> = {
121-
type: 'basic';
122-
default: boolean;
117+
> = BaseConfig<BasicMetadata> & {
123118
/**
124119
* Used by the FE to render form fields.
125120
* E.g. Asking for username/pass
@@ -139,9 +134,7 @@ export type OAuth2AuthConfig<
139134
TAnswers extends Record<string, string> = Record<string, string>,
140135
TOAuth2AppMeta extends Record<string, unknown> = Record<string, unknown>,
141136
TOAuth2CallbackArgs extends Record<string, unknown> = Record<string, unknown>,
142-
> = {
143-
type: 'oauth2';
144-
default: boolean;
137+
> = BaseConfig<OAuth2Metadata> & {
145138
authUrl: (options: {
146139
answers: TAnswers;
147140
/** @deprecated */

0 commit comments

Comments
 (0)