api-def 0.9.1 → 0.9.3

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.
Files changed (122) hide show
  1. package/README.md +1 -1
  2. package/cjs/Api.d.ts +35 -35
  3. package/cjs/Api.js +140 -144
  4. package/cjs/ApiConstants.d.ts +58 -58
  5. package/cjs/ApiConstants.js +55 -55
  6. package/cjs/ApiTypes.d.ts +103 -103
  7. package/cjs/ApiTypes.js +4 -4
  8. package/cjs/ApiUtils.d.ts +6 -6
  9. package/cjs/ApiUtils.js +51 -53
  10. package/cjs/Endpoint.d.ts +52 -52
  11. package/cjs/Endpoint.js +104 -108
  12. package/cjs/EndpointBuilder.d.ts +14 -14
  13. package/cjs/EndpointBuilder.js +46 -46
  14. package/cjs/MockingTypes.d.ts +30 -30
  15. package/cjs/MockingTypes.js +2 -2
  16. package/cjs/QueryHandling.d.ts +3 -3
  17. package/cjs/QueryHandling.js +24 -24
  18. package/cjs/RequestConfig.d.ts +2 -2
  19. package/cjs/RequestConfig.js +74 -74
  20. package/cjs/RequestContext.d.ts +48 -48
  21. package/cjs/RequestContext.js +235 -238
  22. package/cjs/RequestError.d.ts +30 -30
  23. package/cjs/RequestError.js +47 -47
  24. package/cjs/Requester.d.ts +3 -3
  25. package/cjs/Requester.js +335 -336
  26. package/cjs/TextDecoding.d.ts +1 -1
  27. package/cjs/TextDecoding.js +152 -139
  28. package/cjs/UtilTypes.d.ts +5 -5
  29. package/cjs/UtilTypes.js +2 -2
  30. package/cjs/Utils.d.ts +15 -15
  31. package/cjs/Utils.js +79 -77
  32. package/cjs/Validation.d.ts +8 -8
  33. package/cjs/Validation.js +2 -2
  34. package/cjs/backend/AxiosRequestBackend.d.ts +13 -13
  35. package/cjs/backend/AxiosRequestBackend.js +99 -99
  36. package/cjs/backend/FetchRequestBackend.d.ts +15 -15
  37. package/cjs/backend/FetchRequestBackend.js +203 -202
  38. package/cjs/backend/MockRequestBackend.d.ts +11 -11
  39. package/cjs/backend/MockRequestBackend.js +164 -164
  40. package/cjs/backend/RequestBackend.d.ts +19 -19
  41. package/cjs/backend/RequestBackend.js +2 -2
  42. package/cjs/cache/CacheBackend.d.ts +6 -6
  43. package/cjs/cache/CacheBackend.js +2 -2
  44. package/cjs/cache/Caching.d.ts +10 -10
  45. package/cjs/cache/Caching.js +88 -88
  46. package/cjs/cache/LocalForageCacheBackend.d.ts +9 -9
  47. package/cjs/cache/LocalForageCacheBackend.js +24 -24
  48. package/cjs/cache/LocalStorageCacheBackend.d.ts +7 -7
  49. package/cjs/cache/LocalStorageCacheBackend.js +77 -77
  50. package/cjs/index.d.ts +13 -13
  51. package/cjs/index.js +41 -41
  52. package/cjs/middleware/CacheMiddleware.d.ts +7 -7
  53. package/cjs/middleware/CacheMiddleware.js +107 -107
  54. package/cjs/middleware/LoggingMiddleware.d.ts +6 -6
  55. package/cjs/middleware/LoggingMiddleware.js +82 -88
  56. package/cjs/util/retry/index.d.ts +3 -3
  57. package/cjs/util/retry/index.js +52 -52
  58. package/cjs/util/retry/lib/retry.d.ts +3 -3
  59. package/cjs/util/retry/lib/retry.js +50 -54
  60. package/cjs/util/retry/lib/retryOperation.d.ts +27 -27
  61. package/cjs/util/retry/lib/retryOperation.js +130 -134
  62. package/esm/Api.d.ts +35 -35
  63. package/esm/Api.js +92 -98
  64. package/esm/ApiConstants.d.ts +58 -58
  65. package/esm/ApiConstants.js +52 -52
  66. package/esm/ApiTypes.d.ts +103 -103
  67. package/esm/ApiTypes.js +1 -1
  68. package/esm/ApiUtils.d.ts +6 -6
  69. package/esm/ApiUtils.js +43 -45
  70. package/esm/Endpoint.d.ts +52 -52
  71. package/esm/Endpoint.js +66 -70
  72. package/esm/EndpointBuilder.d.ts +14 -14
  73. package/esm/EndpointBuilder.js +31 -31
  74. package/esm/MockingTypes.d.ts +30 -30
  75. package/esm/MockingTypes.js +1 -1
  76. package/esm/QueryHandling.d.ts +3 -3
  77. package/esm/QueryHandling.js +19 -19
  78. package/esm/RequestConfig.d.ts +2 -2
  79. package/esm/RequestConfig.js +56 -56
  80. package/esm/RequestContext.d.ts +48 -48
  81. package/esm/RequestContext.js +167 -170
  82. package/esm/RequestError.d.ts +30 -30
  83. package/esm/RequestError.js +42 -42
  84. package/esm/Requester.d.ts +3 -3
  85. package/esm/Requester.js +252 -253
  86. package/esm/TextDecoding.d.ts +1 -1
  87. package/esm/TextDecoding.js +148 -135
  88. package/esm/UtilTypes.d.ts +5 -5
  89. package/esm/UtilTypes.js +1 -1
  90. package/esm/Utils.d.ts +15 -15
  91. package/esm/Utils.js +66 -68
  92. package/esm/Validation.d.ts +8 -8
  93. package/esm/Validation.js +1 -1
  94. package/esm/backend/AxiosRequestBackend.d.ts +13 -13
  95. package/esm/backend/AxiosRequestBackend.js +62 -62
  96. package/esm/backend/FetchRequestBackend.d.ts +15 -15
  97. package/esm/backend/FetchRequestBackend.js +136 -136
  98. package/esm/backend/MockRequestBackend.d.ts +11 -11
  99. package/esm/backend/MockRequestBackend.js +116 -116
  100. package/esm/backend/RequestBackend.d.ts +19 -19
  101. package/esm/backend/RequestBackend.js +1 -1
  102. package/esm/cache/CacheBackend.d.ts +6 -6
  103. package/esm/cache/CacheBackend.js +1 -1
  104. package/esm/cache/Caching.d.ts +10 -10
  105. package/esm/cache/Caching.js +39 -39
  106. package/esm/cache/LocalForageCacheBackend.d.ts +9 -9
  107. package/esm/cache/LocalForageCacheBackend.js +20 -20
  108. package/esm/cache/LocalStorageCacheBackend.d.ts +7 -7
  109. package/esm/cache/LocalStorageCacheBackend.js +32 -32
  110. package/esm/index.d.ts +13 -13
  111. package/esm/index.js +14 -15
  112. package/esm/middleware/CacheMiddleware.d.ts +7 -7
  113. package/esm/middleware/CacheMiddleware.js +58 -58
  114. package/esm/middleware/LoggingMiddleware.d.ts +6 -6
  115. package/esm/middleware/LoggingMiddleware.js +77 -83
  116. package/esm/util/retry/index.d.ts +3 -3
  117. package/esm/util/retry/index.js +50 -50
  118. package/esm/util/retry/lib/retry.d.ts +3 -3
  119. package/esm/util/retry/lib/retry.js +35 -39
  120. package/esm/util/retry/lib/retryOperation.d.ts +27 -27
  121. package/esm/util/retry/lib/retryOperation.js +126 -132
  122. package/package.json +19 -34
@@ -1,52 +1,52 @@
1
- export const RequestMethod = {
2
- /** @deprecated use 'POST' */
3
- Post: "post",
4
- /** @deprecated use 'GET' */
5
- Get: "get",
6
- POST: "post",
7
- GET: "get",
8
- PUT: "put",
9
- DELETE: "delete",
10
- PATCH: "patch",
11
- };
12
- export const RequestEvent = {
13
- /** @deprecated use 'BEFORE_SEND' */
14
- BeforeSend: "beforeSend",
15
- /** @deprecated use 'SUCCESS' */
16
- Success: "success",
17
- /** @deprecated use 'ERROR' */
18
- Error: "error",
19
- /** @deprecated use 'UNRECOVERABLE_ERROR' */
20
- UnrecoverableError: "unrecoverableError",
21
- BEFORE_SEND: "beforeSend",
22
- SUCCESS: "success",
23
- ERROR: "error",
24
- UNRECOVERABLE_ERROR: "unrecoverableError",
25
- };
26
- export const EventResultType = {
27
- /** @deprecated use 'RESPOND' */
28
- Respond: "respond",
29
- /** @deprecated use 'RETRY' */
30
- Retry: "retry",
31
- RESPOND: "respond",
32
- RETRY: "retry",
33
- };
34
- export const CacheSource = {
35
- /** @deprecated use 'API' */
36
- Api: "api",
37
- /** @deprecated use 'LOCAL' */
38
- Local: "local",
39
- API: "api",
40
- LOCAL: "local",
41
- };
42
- export const ResponseType = {
43
- /** @deprecated use 'JSON' */
44
- Json: "json",
45
- /** @deprecated use 'TEXT' */
46
- Text: "text",
47
- /** @deprecated use 'ARRAY_BUFFER' */
48
- ArrayBuffer: "arraybuffer",
49
- JSON: "json",
50
- TEXT: "text",
51
- ARRAY_BUFFER: "arraybuffer",
52
- };
1
+ export const RequestMethod = {
2
+ /** @deprecated use 'POST' */
3
+ Post: "post",
4
+ /** @deprecated use 'GET' */
5
+ Get: "get",
6
+ POST: "post",
7
+ GET: "get",
8
+ PUT: "put",
9
+ DELETE: "delete",
10
+ PATCH: "patch",
11
+ };
12
+ export const RequestEvent = {
13
+ /** @deprecated use 'BEFORE_SEND' */
14
+ BeforeSend: "beforeSend",
15
+ /** @deprecated use 'SUCCESS' */
16
+ Success: "success",
17
+ /** @deprecated use 'ERROR' */
18
+ Error: "error",
19
+ /** @deprecated use 'UNRECOVERABLE_ERROR' */
20
+ UnrecoverableError: "unrecoverableError",
21
+ BEFORE_SEND: "beforeSend",
22
+ SUCCESS: "success",
23
+ ERROR: "error",
24
+ UNRECOVERABLE_ERROR: "unrecoverableError",
25
+ };
26
+ export const EventResultType = {
27
+ /** @deprecated use 'RESPOND' */
28
+ Respond: "respond",
29
+ /** @deprecated use 'RETRY' */
30
+ Retry: "retry",
31
+ RESPOND: "respond",
32
+ RETRY: "retry",
33
+ };
34
+ export const CacheSource = {
35
+ /** @deprecated use 'API' */
36
+ Api: "api",
37
+ /** @deprecated use 'LOCAL' */
38
+ Local: "local",
39
+ API: "api",
40
+ LOCAL: "local",
41
+ };
42
+ export const ResponseType = {
43
+ /** @deprecated use 'JSON' */
44
+ Json: "json",
45
+ /** @deprecated use 'TEXT' */
46
+ Text: "text",
47
+ /** @deprecated use 'ARRAY_BUFFER' */
48
+ ArrayBuffer: "arraybuffer",
49
+ JSON: "json",
50
+ TEXT: "text",
51
+ ARRAY_BUFFER: "arraybuffer",
52
+ };
package/esm/ApiTypes.d.ts CHANGED
@@ -1,103 +1,103 @@
1
- import RequestContext from "./RequestContext";
2
- import { Api } from "./Api";
3
- import { CacheSource, EventResultType, RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
4
- import RequestBackend from "./backend/RequestBackend";
5
- import { Validation } from "./Validation";
6
- export declare type AcceptableStatus = number | [min: number, max: number];
7
- export declare type Headers = Record<string, string | number | boolean | null | undefined>;
8
- export declare type Params = string;
9
- export declare type Query = string | undefined | Record<string, any>;
10
- export declare type Body = string | number | Record<string, any>;
11
- export interface ApiResponse<T = any> {
12
- readonly method: RequestMethod;
13
- readonly url: string;
14
- readonly status: number;
15
- readonly data: T;
16
- readonly headers: Record<string, string>;
17
- }
18
- export declare type RequestLock = string | false;
19
- export declare type QueryStringify = (query: any) => string;
20
- export declare type QueryParse = (query: string) => any;
21
- interface QueryHandling {
22
- parse: QueryParse;
23
- stringify: QueryStringify;
24
- }
25
- export interface RetryOptions {
26
- maxAttempts: number;
27
- shouldRetry?: (error: Error) => boolean;
28
- minDelay?: number;
29
- maxDelay?: number;
30
- }
31
- export interface BaseRequestConfig {
32
- cache?: number | boolean;
33
- lock?: RequestLock;
34
- retry?: number | false | RetryOptions;
35
- headers?: Readonly<Headers>;
36
- acceptableStatus?: AcceptableStatus[];
37
- /**
38
- * @deprecated use `queryHandling.stringify` instead
39
- **/
40
- queryParser?: QueryStringify;
41
- queryHandling?: Partial<QueryHandling>;
42
- }
43
- export declare type RequestConfig<P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> = (P extends undefined ? {
44
- params?: never;
45
- } : {
46
- params: Record<P extends Params ? P : never, string>;
47
- }) & (Q extends undefined ? {
48
- query?: never;
49
- } : {
50
- query: Q;
51
- }) & (B extends undefined ? {
52
- body?: never;
53
- } : {
54
- body: B;
55
- }) & BaseRequestConfig;
56
- export declare const COMPUTED_CONFIG_SYMBOL: unique symbol;
57
- export declare type ComputedRequestConfig<P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> = Omit<RequestConfig<P, Q, B>, "queryParser" | "query" | "queryHandling"> & {
58
- [COMPUTED_CONFIG_SYMBOL]: true;
59
- queryObject: Record<string, any> | undefined;
60
- queryString: string | undefined;
61
- queryHandling: QueryHandling;
62
- };
63
- interface BaseEventResult<T extends EventResultType> {
64
- type: T;
65
- }
66
- export declare type ResponseEventResult<R> = BaseEventResult<"respond"> & {
67
- response: ApiResponse<R>;
68
- };
69
- export declare type RetryEventResult<R> = BaseEventResult<"retry">;
70
- export declare type EventResult<R> = ResponseEventResult<R> | RetryEventResult<R>;
71
- export declare type RequestEventHandler<R> = (context: RequestContext<R>) => EventResult<R> | void | Promise<EventResult<R> | void>;
72
- export declare type RequestEventHandlers<R> = {
73
- [key in RequestEvent]?: Array<RequestEventHandler<R>>;
74
- };
75
- export declare type RequestMiddleware<O = undefined> = {
76
- [key in RequestEvent]?: RequestEventHandler<any> | undefined | false;
77
- };
78
- export interface RequestContextStats {
79
- cached: false | {
80
- is: true;
81
- by: "local" | "api";
82
- };
83
- attempt: number;
84
- }
85
- export interface RequestHost {
86
- readonly method: RequestMethod;
87
- readonly api: Api;
88
- readonly baseUrl: string;
89
- readonly path: string;
90
- readonly responseType: ResponseType | undefined;
91
- readonly validation: Validation;
92
- computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>): ComputedRequestConfig<P, Q, B>;
93
- computePath(path: string, config: RequestConfig): string;
94
- getRequestBackend(): RequestBackend;
95
- }
96
- export interface CancelledRequestError extends Error {
97
- isCancelledRequest: true;
98
- }
99
- export interface RequestCacheInfo {
100
- cached: boolean;
101
- source: CacheSource | null;
102
- }
103
- export {};
1
+ import { Api } from "./Api";
2
+ import { CacheSource, EventResultType, RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
3
+ import RequestContext from "./RequestContext";
4
+ import { Validation } from "./Validation";
5
+ import RequestBackend from "./backend/RequestBackend";
6
+ export type AcceptableStatus = number | [min: number, max: number];
7
+ export type Headers = Record<string, string | number | boolean | null | undefined>;
8
+ export type Params = string;
9
+ export type Query = string | undefined | Record<string, any>;
10
+ export type Body = string | number | Record<string, any>;
11
+ export interface ApiResponse<T = any> {
12
+ readonly method: RequestMethod;
13
+ readonly url: string;
14
+ readonly status: number;
15
+ readonly data: T;
16
+ readonly headers: Record<string, string>;
17
+ }
18
+ export type RequestLock = string | false;
19
+ export type QueryStringify = (query: any) => string;
20
+ export type QueryParse = (query: string) => any;
21
+ interface QueryHandling {
22
+ parse: QueryParse;
23
+ stringify: QueryStringify;
24
+ }
25
+ export interface RetryOptions {
26
+ maxAttempts: number;
27
+ shouldRetry?: (error: Error) => boolean;
28
+ minDelay?: number;
29
+ maxDelay?: number;
30
+ }
31
+ export interface BaseRequestConfig {
32
+ cache?: number | boolean;
33
+ lock?: RequestLock;
34
+ retry?: number | false | RetryOptions;
35
+ headers?: Readonly<Headers>;
36
+ acceptableStatus?: AcceptableStatus[];
37
+ /**
38
+ * @deprecated use `queryHandling.stringify` instead
39
+ **/
40
+ queryParser?: QueryStringify;
41
+ queryHandling?: Partial<QueryHandling>;
42
+ }
43
+ export type RequestConfig<P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> = (P extends undefined ? {
44
+ params?: never;
45
+ } : {
46
+ params: Record<P extends Params ? P : never, string>;
47
+ }) & (Q extends undefined ? {
48
+ query?: never;
49
+ } : {
50
+ query: Q;
51
+ }) & (B extends undefined ? {
52
+ body?: never;
53
+ } : {
54
+ body: B;
55
+ }) & BaseRequestConfig;
56
+ export declare const COMPUTED_CONFIG_SYMBOL: unique symbol;
57
+ export type ComputedRequestConfig<P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> = Omit<RequestConfig<P, Q, B>, "queryParser" | "query" | "queryHandling"> & {
58
+ [COMPUTED_CONFIG_SYMBOL]: true;
59
+ queryObject: Record<string, any> | undefined;
60
+ queryString: string | undefined;
61
+ queryHandling: QueryHandling;
62
+ };
63
+ interface BaseEventResult<T extends EventResultType> {
64
+ type: T;
65
+ }
66
+ export type ResponseEventResult<R> = BaseEventResult<"respond"> & {
67
+ response: ApiResponse<R>;
68
+ };
69
+ export type RetryEventResult<R> = BaseEventResult<"retry">;
70
+ export type EventResult<R> = ResponseEventResult<R> | RetryEventResult<R>;
71
+ export type RequestEventHandler<R> = (context: RequestContext<R>) => EventResult<R> | void | Promise<EventResult<R> | void>;
72
+ export type RequestEventHandlers<R> = {
73
+ [key in RequestEvent]?: Array<RequestEventHandler<R>>;
74
+ };
75
+ export type RequestMiddleware<O = undefined> = {
76
+ [key in RequestEvent]?: RequestEventHandler<any> | undefined | false;
77
+ };
78
+ export interface RequestContextStats {
79
+ cached: false | {
80
+ is: true;
81
+ by: "local" | "api";
82
+ };
83
+ attempt: number;
84
+ }
85
+ export interface RequestHost {
86
+ readonly method: RequestMethod;
87
+ readonly api: Api;
88
+ readonly baseUrl: string;
89
+ readonly path: string;
90
+ readonly responseType: ResponseType | undefined;
91
+ readonly validation: Validation;
92
+ computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>): ComputedRequestConfig<P, Q, B>;
93
+ computePath(path: string, config: RequestConfig): string;
94
+ getRequestBackend(): RequestBackend;
95
+ }
96
+ export interface CancelledRequestError extends Error {
97
+ isCancelledRequest: true;
98
+ }
99
+ export interface RequestCacheInfo {
100
+ cached: boolean;
101
+ source: CacheSource | null;
102
+ }
103
+ export {};
package/esm/ApiTypes.js CHANGED
@@ -1 +1 @@
1
- export const COMPUTED_CONFIG_SYMBOL = Symbol("computed");
1
+ export const COMPUTED_CONFIG_SYMBOL = Symbol("computed");
package/esm/ApiUtils.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
2
- import { ResponseType } from "./ApiConstants";
3
- export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
4
- export declare const isNetworkError: (error: Error) => boolean;
5
- export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
6
- export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
1
+ import { ResponseType } from "./ApiConstants";
2
+ import { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
3
+ export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
4
+ export declare const isNetworkError: (error: Error) => boolean;
5
+ export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
6
+ export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
package/esm/ApiUtils.js CHANGED
@@ -1,45 +1,43 @@
1
- export const isCancelledError = (error) => {
2
- return "isCancelledRequest" in error;
3
- };
4
- export const isNetworkError = (error) => {
5
- var _a;
6
- return (error.name === "NetworkError" ||
7
- error.message === "Network Error" ||
8
- ((_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name) === "NetworkError");
9
- };
10
- const DEFAULT_ACCEPTABLE_STATUS = [[200, 299], 304];
11
- export const isAcceptableStatus = (status, acceptableStatus) => {
12
- const acceptable = acceptableStatus !== null && acceptableStatus !== void 0 ? acceptableStatus : DEFAULT_ACCEPTABLE_STATUS;
13
- for (const cmpStatus of acceptable) {
14
- if (Array.isArray(cmpStatus)) {
15
- const [min, max] = cmpStatus;
16
- if (status >= min && status <= max) {
17
- return (true);
18
- }
19
- }
20
- else if (!isNaN(cmpStatus)) {
21
- if (status === cmpStatus) {
22
- return (true);
23
- }
24
- }
25
- }
26
- return (false);
27
- };
28
- const TEXT_CONTENT_TYPES = ["text/plain", "text/html", "text/xml", "application/xml"];
29
- const JSON_CONTENT_TYPES = ["text/json", "application/json"];
30
- const ARRAY_BUFFER_CONTENT_TYPES = ["application/octet-stream"];
31
- export const inferResponseType = (contentType) => {
32
- const contentTypePart = contentType === null || contentType === void 0 ? void 0 : contentType.split(";")[0].trim();
33
- if (contentTypePart) {
34
- if (TEXT_CONTENT_TYPES.includes(contentTypePart)) {
35
- return "text";
36
- }
37
- else if (JSON_CONTENT_TYPES.includes(contentTypePart)) {
38
- return "json";
39
- }
40
- else if (ARRAY_BUFFER_CONTENT_TYPES.includes(contentTypePart)) {
41
- return "arraybuffer";
42
- }
43
- }
44
- return "text";
45
- };
1
+ export const isCancelledError = (error) => {
2
+ return "isCancelledRequest" in error;
3
+ };
4
+ export const isNetworkError = (error) => {
5
+ var _a;
6
+ return error.name === "NetworkError" || error.message === "Network Error" || ((_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name) === "NetworkError";
7
+ };
8
+ const DEFAULT_ACCEPTABLE_STATUS = [[200, 299], 304];
9
+ export const isAcceptableStatus = (status, acceptableStatus) => {
10
+ const acceptable = acceptableStatus !== null && acceptableStatus !== void 0 ? acceptableStatus : DEFAULT_ACCEPTABLE_STATUS;
11
+ for (const cmpStatus of acceptable) {
12
+ if (Array.isArray(cmpStatus)) {
13
+ const [min, max] = cmpStatus;
14
+ if (status >= min && status <= max) {
15
+ return true;
16
+ }
17
+ }
18
+ else if (!Number.isNaN(cmpStatus)) {
19
+ if (status === cmpStatus) {
20
+ return true;
21
+ }
22
+ }
23
+ }
24
+ return false;
25
+ };
26
+ const TEXT_CONTENT_TYPES = ["text/plain", "text/html", "text/xml", "application/xml"];
27
+ const JSON_CONTENT_TYPES = ["text/json", "application/json"];
28
+ const ARRAY_BUFFER_CONTENT_TYPES = ["application/octet-stream"];
29
+ export const inferResponseType = (contentType) => {
30
+ const contentTypePart = contentType === null || contentType === void 0 ? void 0 : contentType.split(";")[0].trim();
31
+ if (contentTypePart) {
32
+ if (TEXT_CONTENT_TYPES.includes(contentTypePart)) {
33
+ return "text";
34
+ }
35
+ if (JSON_CONTENT_TYPES.includes(contentTypePart)) {
36
+ return "json";
37
+ }
38
+ if (ARRAY_BUFFER_CONTENT_TYPES.includes(contentTypePart)) {
39
+ return "arraybuffer";
40
+ }
41
+ }
42
+ return "text";
43
+ };
package/esm/Endpoint.d.ts CHANGED
@@ -1,52 +1,52 @@
1
- import { Api } from "./Api";
2
- import { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RequestConfig, RequestHost } from "./ApiTypes";
3
- import * as Mocking from "./MockingTypes";
4
- import { RequestMethod, ResponseType } from "./ApiConstants";
5
- import RequestBackend from "./backend/RequestBackend";
6
- import { Validation } from "./Validation";
7
- export interface EndpointConfig<R, P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined, Path extends string = string> {
8
- readonly id: string;
9
- readonly method: RequestMethod;
10
- readonly path: Path;
11
- /**
12
- * Name your endpoint to help with debugging and documentation
13
- * @default `id` is used as the name if no name is supplied
14
- */
15
- readonly name?: string;
16
- /**
17
- * Describe your endpoint to help with debugging and documentation
18
- */
19
- readonly description?: string;
20
- readonly config?: BaseRequestConfig;
21
- /**
22
- * Let the backend requestor (fetch, axios, etc.) know what type of response
23
- * you are expecting from this endpoint
24
- * @default `application/json`
25
- */
26
- readonly responseType?: ResponseType;
27
- /**
28
- * Specify mocking for your endpoint upfront to help with disconnected dev,
29
- * and testing.
30
- * Enable/disable mocked returns for all endpoints on your API object.
31
- */
32
- readonly mocking?: Mocking.EndpointMockingConfig<R, P, Q, B>;
33
- readonly validation?: Validation<R, P, Q, B>;
34
- }
35
- export default class Endpoint<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> implements EndpointConfig<R, P, Q, B>, RequestHost {
36
- readonly api: Api;
37
- readonly id: string;
38
- readonly method: RequestMethod;
39
- readonly name: string;
40
- readonly description?: string;
41
- readonly path: string;
42
- readonly config?: BaseRequestConfig;
43
- readonly responseType: ResponseType | undefined;
44
- readonly mocking?: Mocking.EndpointMockingConfig<R, P, Q, B>;
45
- readonly validation: Validation<R, P, Q, B>;
46
- constructor(api: Api, info: EndpointConfig<R, P, Q, B>);
47
- submit(config: RequestConfig<P, Q, B>): Promise<ApiResponse<R>>;
48
- computePath(path: string, request: RequestConfig): string;
49
- get baseUrl(): string;
50
- computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>): ComputedRequestConfig<P, Q, B>;
51
- getRequestBackend(): RequestBackend;
52
- }
1
+ import { Api } from "./Api";
2
+ import { RequestMethod, ResponseType } from "./ApiConstants";
3
+ import { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RequestConfig, RequestHost } from "./ApiTypes";
4
+ import * as Mocking from "./MockingTypes";
5
+ import { Validation } from "./Validation";
6
+ import RequestBackend from "./backend/RequestBackend";
7
+ export interface EndpointConfig<R, P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined, Path extends string = string> {
8
+ readonly id: string;
9
+ readonly method: RequestMethod;
10
+ readonly path: Path;
11
+ /**
12
+ * Name your endpoint to help with debugging and documentation
13
+ * @default `id` is used as the name if no name is supplied
14
+ */
15
+ readonly name?: string;
16
+ /**
17
+ * Describe your endpoint to help with debugging and documentation
18
+ */
19
+ readonly description?: string;
20
+ readonly config?: BaseRequestConfig;
21
+ /**
22
+ * Let the backend requestor (fetch, axios, etc.) know what type of response
23
+ * you are expecting from this endpoint
24
+ * @default `application/json`
25
+ */
26
+ readonly responseType?: ResponseType;
27
+ /**
28
+ * Specify mocking for your endpoint upfront to help with disconnected dev,
29
+ * and testing.
30
+ * Enable/disable mocked returns for all endpoints on your API object.
31
+ */
32
+ readonly mocking?: Mocking.EndpointMockingConfig<R, P, Q, B>;
33
+ readonly validation?: Validation<R, P, Q, B>;
34
+ }
35
+ export default class Endpoint<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> implements EndpointConfig<R, P, Q, B>, RequestHost {
36
+ readonly api: Api;
37
+ readonly id: string;
38
+ readonly method: RequestMethod;
39
+ readonly name: string;
40
+ readonly description?: string;
41
+ readonly path: string;
42
+ readonly config?: BaseRequestConfig;
43
+ readonly responseType: ResponseType | undefined;
44
+ readonly mocking?: Mocking.EndpointMockingConfig<R, P, Q, B>;
45
+ readonly validation: Validation<R, P, Q, B>;
46
+ constructor(api: Api, info: EndpointConfig<R, P, Q, B>);
47
+ submit(config: RequestConfig<P, Q, B>): Promise<ApiResponse<R>>;
48
+ computePath(path: string, request: RequestConfig): string;
49
+ get baseUrl(): string;
50
+ computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>): ComputedRequestConfig<P, Q, B>;
51
+ getRequestBackend(): RequestBackend;
52
+ }