fets 0.4.1-alpha-20230714103147-e36adcc → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OAuthPathErrorType = void 0;
4
+ var OAuthPathErrorType;
5
+ (function (OAuthPathErrorType) {
6
+ /**
7
+ * The request is missing a parameter so the server can’t proceed with the request.
8
+ * This may also be returned if the request includes an unsupported parameter or repeats a parameter.
9
+ */
10
+ OAuthPathErrorType["invalid_request"] = "invalid_request";
11
+ /**
12
+ * Client authentication failed, such as if the request contains an invalid client ID or secret.
13
+ * Send an HTTP 401 response in this case.
14
+ */
15
+ OAuthPathErrorType["invalid_client"] = "invalid_client";
16
+ /**
17
+ * The authorization code (or user’s password for the password grant type) is invalid or expired.
18
+ * This is also the error you would return if the redirect URL given in the authorization grant does not match the URL provided in this access token request.
19
+ */
20
+ OAuthPathErrorType["invalid_grant"] = "invalid_grant";
21
+ /**
22
+ * For access token requests that include a scope (password or client_credentials grants), this error indicates an invalid scope value in the request.
23
+ */
24
+ OAuthPathErrorType["invalid_scope"] = "invalid_scope";
25
+ /**
26
+ * This client is not authorized to use the requested grant type. For example, if you restrict which applications can use the Implicit grant, you would return this error for the other apps.
27
+ */
28
+ OAuthPathErrorType["unauthorized_client"] = "unauthorized_client";
29
+ /**
30
+ * If a grant type is requested that the authorization server doesn’t recognize, use this code.
31
+ * Note that unknown grant types also use this specific error code rather than using the `invalid_request` above.
32
+ */
33
+ OAuthPathErrorType["unsupported_grant_type"] = "unsupported_grant_type";
34
+ })(OAuthPathErrorType || (exports.OAuthPathErrorType = OAuthPathErrorType = {}));
@@ -54,7 +54,7 @@ function createClient({ endpoint, fetchFn = fetch_1.fetch, plugins = [] }) {
54
54
  path = path.replace(`{${pathParamKey}}`, value).replace(`:${pathParamKey}`, value);
55
55
  }
56
56
  }
57
- if (!path.startsWith('/')) {
57
+ if (!path.startsWith('/') && !path.startsWith('http')) {
58
58
  path = `/${path}`;
59
59
  }
60
60
  let searchParams;
@@ -116,7 +116,7 @@ function createClient({ endpoint, fetchFn = fetch_1.fetch, plugins = [] }) {
116
116
  });
117
117
  }
118
118
  let finalUrl = path;
119
- if (endpoint) {
119
+ if (endpoint && !path.startsWith('http')) {
120
120
  finalUrl = `${endpoint}${path}`;
121
121
  }
122
122
  if (searchParams) {
@@ -0,0 +1,31 @@
1
+ export var OAuthPathErrorType;
2
+ (function (OAuthPathErrorType) {
3
+ /**
4
+ * The request is missing a parameter so the server can’t proceed with the request.
5
+ * This may also be returned if the request includes an unsupported parameter or repeats a parameter.
6
+ */
7
+ OAuthPathErrorType["invalid_request"] = "invalid_request";
8
+ /**
9
+ * Client authentication failed, such as if the request contains an invalid client ID or secret.
10
+ * Send an HTTP 401 response in this case.
11
+ */
12
+ OAuthPathErrorType["invalid_client"] = "invalid_client";
13
+ /**
14
+ * The authorization code (or user’s password for the password grant type) is invalid or expired.
15
+ * This is also the error you would return if the redirect URL given in the authorization grant does not match the URL provided in this access token request.
16
+ */
17
+ OAuthPathErrorType["invalid_grant"] = "invalid_grant";
18
+ /**
19
+ * For access token requests that include a scope (password or client_credentials grants), this error indicates an invalid scope value in the request.
20
+ */
21
+ OAuthPathErrorType["invalid_scope"] = "invalid_scope";
22
+ /**
23
+ * This client is not authorized to use the requested grant type. For example, if you restrict which applications can use the Implicit grant, you would return this error for the other apps.
24
+ */
25
+ OAuthPathErrorType["unauthorized_client"] = "unauthorized_client";
26
+ /**
27
+ * If a grant type is requested that the authorization server doesn’t recognize, use this code.
28
+ * Note that unknown grant types also use this specific error code rather than using the `invalid_request` above.
29
+ */
30
+ OAuthPathErrorType["unsupported_grant_type"] = "unsupported_grant_type";
31
+ })(OAuthPathErrorType || (OAuthPathErrorType = {}));
@@ -50,7 +50,7 @@ export function createClient({ endpoint, fetchFn = fetch, plugins = [] }) {
50
50
  path = path.replace(`{${pathParamKey}}`, value).replace(`:${pathParamKey}`, value);
51
51
  }
52
52
  }
53
- if (!path.startsWith('/')) {
53
+ if (!path.startsWith('/') && !path.startsWith('http')) {
54
54
  path = `/${path}`;
55
55
  }
56
56
  let searchParams;
@@ -112,7 +112,7 @@ export function createClient({ endpoint, fetchFn = fetch, plugins = [] }) {
112
112
  });
113
113
  }
114
114
  let finalUrl = path;
115
- if (endpoint) {
115
+ if (endpoint && !path.startsWith('http')) {
116
116
  finalUrl = `${endpoint}${path}`;
117
117
  }
118
118
  if (searchParams) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fets",
3
- "version": "0.4.1-alpha-20230714103147-e36adcc",
3
+ "version": "0.4.2",
4
4
  "description": "TypeScript HTTP Framework focusing on e2e type-safety, easy setup, performance & great developer experience",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -0,0 +1,112 @@
1
+ import type { TypedResponseWithJSONStatusMap } from '../../typed-fetch.cjs';
2
+ export type OAuth2AuthParams<TSecurityScheme> = TSecurityScheme extends {
3
+ type: 'oauth2';
4
+ } ? {
5
+ headers: {
6
+ Authorization: `Bearer ${string}`;
7
+ };
8
+ } : {};
9
+ export type OASOAuthPathRequestParamsWithHeader = {
10
+ formUrlEncoded: {
11
+ grant_type: 'client_credentials';
12
+ };
13
+ headers: {
14
+ /**
15
+ * The client ID and secret can be sent in the HTTP Basic auth header.
16
+ * @example `Authorization: Basic <base64 encoded client_id:client_secret>`
17
+ */
18
+ Authorization: `Basic ${string}`;
19
+ };
20
+ };
21
+ export type OASOAuthPathRequestParamsWithoutHeader = {
22
+ grant_type: 'client_credentials';
23
+ client_id: string;
24
+ client_secret: string;
25
+ };
26
+ export type OASOAuthPath<TOAS> = TOAS extends {
27
+ components: {
28
+ securitySchemes: {
29
+ [key: string]: {
30
+ type: 'oauth2';
31
+ flows: {
32
+ authorizationCode: {
33
+ tokenUrl: infer TTokenURL;
34
+ };
35
+ };
36
+ };
37
+ };
38
+ };
39
+ } ? {
40
+ [TPath in TTokenURL extends string ? TTokenURL : never]: {
41
+ /**
42
+ * The client needs to authenticate themselves for this request.
43
+ * Typically the service will allow either additional request parameters `client_id` and `client_secret`,
44
+ * or accept the client ID and secret in the HTTP Basic auth header.
45
+ */
46
+ post(requestParams: OASOAuthPathRequestParamsWithHeader | OASOAuthPathRequestParamsWithoutHeader, requestInit?: RequestInit): Promise<TypedResponseWithJSONStatusMap<{
47
+ 200: OAuthPathSuccessResponse;
48
+ 400: OAuthPathFailedResponse;
49
+ }>>;
50
+ };
51
+ } : {};
52
+ export type OAuthPathSuccessResponse = {
53
+ /**
54
+ * The access token string as issued by the authorization server.
55
+ */
56
+ access_token: string;
57
+ /**
58
+ * The type of token this is, typically just the string “Bearer”.
59
+ */
60
+ token_type: 'Bearer' | 'bearer';
61
+ /**
62
+ * If the access token expires, the server should reply with the duration of time the access token is granted for.
63
+ */
64
+ expires_in?: number;
65
+ /**
66
+ * If the access token will expire,
67
+ * then it is useful to return a refresh token which applications can use to obtain another access token.
68
+ * However, tokens issued with the implicit grant cannot be issued a refresh token.
69
+ */
70
+ refresh_token?: string;
71
+ /**
72
+ * If the scope the user granted is identical to the scope the app requested, this parameter is optional.
73
+ * If the granted scope is different from the requested scope,
74
+ * such as if the user modified the scope, then this parameter is required.
75
+ */
76
+ scope?: string;
77
+ };
78
+ export interface OAuthPathFailedResponse {
79
+ error: OAuthPathErrorType;
80
+ error_description?: string;
81
+ error_uri?: string;
82
+ }
83
+ export declare enum OAuthPathErrorType {
84
+ /**
85
+ * The request is missing a parameter so the server can’t proceed with the request.
86
+ * This may also be returned if the request includes an unsupported parameter or repeats a parameter.
87
+ */
88
+ invalid_request = "invalid_request",
89
+ /**
90
+ * Client authentication failed, such as if the request contains an invalid client ID or secret.
91
+ * Send an HTTP 401 response in this case.
92
+ */
93
+ invalid_client = "invalid_client",
94
+ /**
95
+ * The authorization code (or user’s password for the password grant type) is invalid or expired.
96
+ * This is also the error you would return if the redirect URL given in the authorization grant does not match the URL provided in this access token request.
97
+ */
98
+ invalid_grant = "invalid_grant",
99
+ /**
100
+ * For access token requests that include a scope (password or client_credentials grants), this error indicates an invalid scope value in the request.
101
+ */
102
+ invalid_scope = "invalid_scope",
103
+ /**
104
+ * This client is not authorized to use the requested grant type. For example, if you restrict which applications can use the Implicit grant, you would return this error for the other apps.
105
+ */
106
+ unauthorized_client = "unauthorized_client",
107
+ /**
108
+ * If a grant type is requested that the authorization server doesn’t recognize, use this code.
109
+ * Note that unknown grant types also use this specific error code rather than using the `invalid_request` above.
110
+ */
111
+ unsupported_grant_type = "unsupported_grant_type"
112
+ }
@@ -0,0 +1,112 @@
1
+ import type { TypedResponseWithJSONStatusMap } from '../../typed-fetch.js';
2
+ export type OAuth2AuthParams<TSecurityScheme> = TSecurityScheme extends {
3
+ type: 'oauth2';
4
+ } ? {
5
+ headers: {
6
+ Authorization: `Bearer ${string}`;
7
+ };
8
+ } : {};
9
+ export type OASOAuthPathRequestParamsWithHeader = {
10
+ formUrlEncoded: {
11
+ grant_type: 'client_credentials';
12
+ };
13
+ headers: {
14
+ /**
15
+ * The client ID and secret can be sent in the HTTP Basic auth header.
16
+ * @example `Authorization: Basic <base64 encoded client_id:client_secret>`
17
+ */
18
+ Authorization: `Basic ${string}`;
19
+ };
20
+ };
21
+ export type OASOAuthPathRequestParamsWithoutHeader = {
22
+ grant_type: 'client_credentials';
23
+ client_id: string;
24
+ client_secret: string;
25
+ };
26
+ export type OASOAuthPath<TOAS> = TOAS extends {
27
+ components: {
28
+ securitySchemes: {
29
+ [key: string]: {
30
+ type: 'oauth2';
31
+ flows: {
32
+ authorizationCode: {
33
+ tokenUrl: infer TTokenURL;
34
+ };
35
+ };
36
+ };
37
+ };
38
+ };
39
+ } ? {
40
+ [TPath in TTokenURL extends string ? TTokenURL : never]: {
41
+ /**
42
+ * The client needs to authenticate themselves for this request.
43
+ * Typically the service will allow either additional request parameters `client_id` and `client_secret`,
44
+ * or accept the client ID and secret in the HTTP Basic auth header.
45
+ */
46
+ post(requestParams: OASOAuthPathRequestParamsWithHeader | OASOAuthPathRequestParamsWithoutHeader, requestInit?: RequestInit): Promise<TypedResponseWithJSONStatusMap<{
47
+ 200: OAuthPathSuccessResponse;
48
+ 400: OAuthPathFailedResponse;
49
+ }>>;
50
+ };
51
+ } : {};
52
+ export type OAuthPathSuccessResponse = {
53
+ /**
54
+ * The access token string as issued by the authorization server.
55
+ */
56
+ access_token: string;
57
+ /**
58
+ * The type of token this is, typically just the string “Bearer”.
59
+ */
60
+ token_type: 'Bearer' | 'bearer';
61
+ /**
62
+ * If the access token expires, the server should reply with the duration of time the access token is granted for.
63
+ */
64
+ expires_in?: number;
65
+ /**
66
+ * If the access token will expire,
67
+ * then it is useful to return a refresh token which applications can use to obtain another access token.
68
+ * However, tokens issued with the implicit grant cannot be issued a refresh token.
69
+ */
70
+ refresh_token?: string;
71
+ /**
72
+ * If the scope the user granted is identical to the scope the app requested, this parameter is optional.
73
+ * If the granted scope is different from the requested scope,
74
+ * such as if the user modified the scope, then this parameter is required.
75
+ */
76
+ scope?: string;
77
+ };
78
+ export interface OAuthPathFailedResponse {
79
+ error: OAuthPathErrorType;
80
+ error_description?: string;
81
+ error_uri?: string;
82
+ }
83
+ export declare enum OAuthPathErrorType {
84
+ /**
85
+ * The request is missing a parameter so the server can’t proceed with the request.
86
+ * This may also be returned if the request includes an unsupported parameter or repeats a parameter.
87
+ */
88
+ invalid_request = "invalid_request",
89
+ /**
90
+ * Client authentication failed, such as if the request contains an invalid client ID or secret.
91
+ * Send an HTTP 401 response in this case.
92
+ */
93
+ invalid_client = "invalid_client",
94
+ /**
95
+ * The authorization code (or user’s password for the password grant type) is invalid or expired.
96
+ * This is also the error you would return if the redirect URL given in the authorization grant does not match the URL provided in this access token request.
97
+ */
98
+ invalid_grant = "invalid_grant",
99
+ /**
100
+ * For access token requests that include a scope (password or client_credentials grants), this error indicates an invalid scope value in the request.
101
+ */
102
+ invalid_scope = "invalid_scope",
103
+ /**
104
+ * This client is not authorized to use the requested grant type. For example, if you restrict which applications can use the Implicit grant, you would return this error for the other apps.
105
+ */
106
+ unauthorized_client = "unauthorized_client",
107
+ /**
108
+ * If a grant type is requested that the authorization server doesn’t recognize, use this code.
109
+ * Note that unknown grant types also use this specific error code rather than using the `invalid_request` above.
110
+ */
111
+ unsupported_grant_type = "unsupported_grant_type"
112
+ }
@@ -1,7 +1,8 @@
1
- import { B, Call, Pipe, Strings, Tuples } from 'hotscript';
2
- import { O } from 'ts-toolbelt';
1
+ import type { B, Call, Pipe, Strings, Tuples } from 'hotscript';
2
+ import type { O } from 'ts-toolbelt';
3
3
  import { HTTPMethod, NotOkStatusCode, StatusCode, TypedResponse } from '../typed-fetch.cjs';
4
- import { FromSchema, JSONSchema, OpenAPIDocument } from '../types.cjs';
4
+ import type { FromSchema, JSONSchema, OpenAPIDocument } from '../types.cjs';
5
+ import type { OASOAuthPath, OAuth2AuthParams } from './auth/oauth.cjs';
5
6
  type JSONSchema7TypeName = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
6
7
  type Mutable<Type> = {
7
8
  -readonly [Key in keyof Type]: Mutable<Type[Key]>;
@@ -87,7 +88,7 @@ export type OASClient<TOAS extends OpenAPIDocument> = {
87
88
  query: {};
88
89
  } ? (requestParams: OASRequestParams<TOAS, TPath, TMethod>, init?: RequestInit) => Promise<OASResponse<TOAS, TPath, TMethod>> : (requestParams?: OASRequestParams<TOAS, TPath, TMethod>, init?: RequestInit) => Promise<OASResponse<TOAS, TPath, TMethod>>;
89
90
  };
90
- };
91
+ } & OASOAuthPath<TOAS>;
91
92
  export type OASModel<TOAS extends OpenAPIDocument, TName extends string> = TOAS extends {
92
93
  components: {
93
94
  schemas: {
@@ -293,12 +294,5 @@ export type ApiKeyAuthParams<TSecurityScheme> = TSecurityScheme extends {
293
294
  [TQueryName in TApiKeyQueryName extends string ? TApiKeyQueryName : never]: string;
294
295
  };
295
296
  } : {};
296
- export type OAuth2AuthParams<TSecurityScheme> = TSecurityScheme extends {
297
- type: 'oauth2';
298
- } ? {
299
- headers: {
300
- Authorization: `Bearer ${string}`;
301
- };
302
- } : {};
303
297
  export type OASSecurityParams<TSecurityScheme> = BasicAuthParams<TSecurityScheme> & BearerAuthParams<TSecurityScheme> & ApiKeyAuthParams<TSecurityScheme> & OAuth2AuthParams<TSecurityScheme>;
304
298
  export {};
@@ -1,7 +1,8 @@
1
- import { B, Call, Pipe, Strings, Tuples } from 'hotscript';
2
- import { O } from 'ts-toolbelt';
1
+ import type { B, Call, Pipe, Strings, Tuples } from 'hotscript';
2
+ import type { O } from 'ts-toolbelt';
3
3
  import { HTTPMethod, NotOkStatusCode, StatusCode, TypedResponse } from '../typed-fetch.js';
4
- import { FromSchema, JSONSchema, OpenAPIDocument } from '../types.js';
4
+ import type { FromSchema, JSONSchema, OpenAPIDocument } from '../types.js';
5
+ import type { OASOAuthPath, OAuth2AuthParams } from './auth/oauth.js';
5
6
  type JSONSchema7TypeName = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
6
7
  type Mutable<Type> = {
7
8
  -readonly [Key in keyof Type]: Mutable<Type[Key]>;
@@ -87,7 +88,7 @@ export type OASClient<TOAS extends OpenAPIDocument> = {
87
88
  query: {};
88
89
  } ? (requestParams: OASRequestParams<TOAS, TPath, TMethod>, init?: RequestInit) => Promise<OASResponse<TOAS, TPath, TMethod>> : (requestParams?: OASRequestParams<TOAS, TPath, TMethod>, init?: RequestInit) => Promise<OASResponse<TOAS, TPath, TMethod>>;
89
90
  };
90
- };
91
+ } & OASOAuthPath<TOAS>;
91
92
  export type OASModel<TOAS extends OpenAPIDocument, TName extends string> = TOAS extends {
92
93
  components: {
93
94
  schemas: {
@@ -293,12 +294,5 @@ export type ApiKeyAuthParams<TSecurityScheme> = TSecurityScheme extends {
293
294
  [TQueryName in TApiKeyQueryName extends string ? TApiKeyQueryName : never]: string;
294
295
  };
295
296
  } : {};
296
- export type OAuth2AuthParams<TSecurityScheme> = TSecurityScheme extends {
297
- type: 'oauth2';
298
- } ? {
299
- headers: {
300
- Authorization: `Bearer ${string}`;
301
- };
302
- } : {};
303
297
  export type OASSecurityParams<TSecurityScheme> = BasicAuthParams<TSecurityScheme> & BearerAuthParams<TSecurityScheme> & ApiKeyAuthParams<TSecurityScheme> & OAuth2AuthParams<TSecurityScheme>;
304
298
  export {};
@@ -46,7 +46,7 @@ export type TypedResponseCtor = Omit<typeof Response, 'json'> & {
46
46
  redirect(url: string | URL): TypedResponse<any, Record<string, string>, 302>;
47
47
  };
48
48
  export type TypedResponseWithJSONStatusMap<TResponseJSONStatusMap extends StatusCodeMap<any>> = {
49
- [TStatusCode in keyof TResponseJSONStatusMap]?: TStatusCode extends StatusCode ? TypedResponse<TResponseJSONStatusMap[TStatusCode], Record<string, string>, TStatusCode> : never;
49
+ [TStatusCode in keyof TResponseJSONStatusMap]: TStatusCode extends StatusCode ? TypedResponse<TResponseJSONStatusMap[TStatusCode], Record<string, string>, TStatusCode> : never;
50
50
  }[keyof TResponseJSONStatusMap];
51
51
  export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE';
52
52
  export type TypedRequestInit<THeaders extends Record<string, string>, TMethod extends HTTPMethod, TFormData extends Record<string, FormDataEntryValue>> = Omit<RequestInit, 'method' | 'headers' | 'body'> & {
@@ -46,7 +46,7 @@ export type TypedResponseCtor = Omit<typeof Response, 'json'> & {
46
46
  redirect(url: string | URL): TypedResponse<any, Record<string, string>, 302>;
47
47
  };
48
48
  export type TypedResponseWithJSONStatusMap<TResponseJSONStatusMap extends StatusCodeMap<any>> = {
49
- [TStatusCode in keyof TResponseJSONStatusMap]?: TStatusCode extends StatusCode ? TypedResponse<TResponseJSONStatusMap[TStatusCode], Record<string, string>, TStatusCode> : never;
49
+ [TStatusCode in keyof TResponseJSONStatusMap]: TStatusCode extends StatusCode ? TypedResponse<TResponseJSONStatusMap[TStatusCode], Record<string, string>, TStatusCode> : never;
50
50
  }[keyof TResponseJSONStatusMap];
51
51
  export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE';
52
52
  export type TypedRequestInit<THeaders extends Record<string, string>, TMethod extends HTTPMethod, TFormData extends Record<string, FormDataEntryValue>> = Omit<RequestInit, 'method' | 'headers' | 'body'> & {