@readme/api-core 7.0.0-beta.1 → 7.0.0-beta.10

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.
package/dist/index.d.cts CHANGED
@@ -1,40 +1,41 @@
1
- import Operation from 'oas/operation';
2
- import { OASDocument, HttpMethods } from 'oas/rmoas.types';
1
+ import * as har_format from 'har-format';
2
+ import { ConfigOptions } from './types.cjs';
3
+ import { DataForHAR, AuthForHAR } from '@readme/oas-to-har/lib/types';
4
+ import { Operation } from 'oas/operation';
5
+ import { OASDocument, HttpMethods } from 'oas/types';
3
6
  import Oas from 'oas';
7
+ import 'json-schema-to-ts';
4
8
 
5
- interface ConfigOptions {
6
- /**
7
- * Override the default `fetch` request timeout of 30 seconds. This number should be represented
8
- * in milliseconds.
9
- */
10
- timeout?: number;
11
- }
12
- interface FetchResponse<HTTPStatus, Data> {
13
- data: Data;
14
- headers: Headers;
15
- res: Response;
16
- status: HTTPStatus;
17
- }
18
- type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc['length']]>;
19
- type HTTPMethodRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;
20
9
  declare class APICore {
21
10
  spec: Oas;
22
11
  private auth;
23
12
  private server;
24
13
  private config;
25
14
  private userAgent;
26
- constructor(definition?: Record<string, unknown> | OASDocument, userAgent?: string);
15
+ constructor(definition?: OASDocument | Record<string, unknown>, userAgent?: string);
27
16
  setSpec(spec: Oas): void;
28
17
  setConfig(config: ConfigOptions): this;
29
18
  setUserAgent(userAgent: string): this;
30
- setAuth(...values: string[] | number[]): this;
31
- setServer(url: string, variables?: Record<string, string | number>): this;
19
+ setAuth(...values: number[] | string[]): this;
20
+ setServer(url: string, variables?: Record<string, number | string>): this;
32
21
  fetch<HTTPStatus extends number = number>(path: string, method: HttpMethods, body?: unknown, metadata?: Record<string, unknown>): Promise<{
33
22
  data: any;
34
23
  status: HTTPStatus;
35
24
  headers: Headers;
36
25
  res: Response;
37
26
  }>;
27
+ /**
28
+ * Retrieve a HAR for a given HTTP request.
29
+ *
30
+ * @internal
31
+ */
32
+ getHARForRequest(operation: Operation, data: DataForHAR, auth: AuthForHAR): {
33
+ log: {
34
+ entries: {
35
+ request: har_format.Request;
36
+ }[];
37
+ };
38
+ };
38
39
  fetchOperation<HTTPStatus extends number = number>(operation: Operation, body?: unknown, metadata?: Record<string, unknown>): Promise<{
39
40
  data: any;
40
41
  status: HTTPStatus;
@@ -43,4 +44,4 @@ declare class APICore {
43
44
  }>;
44
45
  }
45
46
 
46
- export { ConfigOptions, FetchResponse, HTTPMethodRange, APICore as default };
47
+ export = APICore;
package/dist/index.d.ts CHANGED
@@ -1,40 +1,41 @@
1
- import Operation from 'oas/operation';
2
- import { OASDocument, HttpMethods } from 'oas/rmoas.types';
1
+ import * as har_format from 'har-format';
2
+ import { ConfigOptions } from './types.js';
3
+ import { DataForHAR, AuthForHAR } from '@readme/oas-to-har/lib/types';
4
+ import { Operation } from 'oas/operation';
5
+ import { OASDocument, HttpMethods } from 'oas/types';
3
6
  import Oas from 'oas';
7
+ import 'json-schema-to-ts';
4
8
 
5
- interface ConfigOptions {
6
- /**
7
- * Override the default `fetch` request timeout of 30 seconds. This number should be represented
8
- * in milliseconds.
9
- */
10
- timeout?: number;
11
- }
12
- interface FetchResponse<HTTPStatus, Data> {
13
- data: Data;
14
- headers: Headers;
15
- res: Response;
16
- status: HTTPStatus;
17
- }
18
- type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc['length']]>;
19
- type HTTPMethodRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;
20
9
  declare class APICore {
21
10
  spec: Oas;
22
11
  private auth;
23
12
  private server;
24
13
  private config;
25
14
  private userAgent;
26
- constructor(definition?: Record<string, unknown> | OASDocument, userAgent?: string);
15
+ constructor(definition?: OASDocument | Record<string, unknown>, userAgent?: string);
27
16
  setSpec(spec: Oas): void;
28
17
  setConfig(config: ConfigOptions): this;
29
18
  setUserAgent(userAgent: string): this;
30
- setAuth(...values: string[] | number[]): this;
31
- setServer(url: string, variables?: Record<string, string | number>): this;
19
+ setAuth(...values: number[] | string[]): this;
20
+ setServer(url: string, variables?: Record<string, number | string>): this;
32
21
  fetch<HTTPStatus extends number = number>(path: string, method: HttpMethods, body?: unknown, metadata?: Record<string, unknown>): Promise<{
33
22
  data: any;
34
23
  status: HTTPStatus;
35
24
  headers: Headers;
36
25
  res: Response;
37
26
  }>;
27
+ /**
28
+ * Retrieve a HAR for a given HTTP request.
29
+ *
30
+ * @internal
31
+ */
32
+ getHARForRequest(operation: Operation, data: DataForHAR, auth: AuthForHAR): {
33
+ log: {
34
+ entries: {
35
+ request: har_format.Request;
36
+ }[];
37
+ };
38
+ };
38
39
  fetchOperation<HTTPStatus extends number = number>(operation: Operation, body?: unknown, metadata?: Record<string, unknown>): Promise<{
39
40
  data: any;
40
41
  status: HTTPStatus;
@@ -43,4 +44,4 @@ declare class APICore {
43
44
  }>;
44
45
  }
45
46
 
46
- export { ConfigOptions, FetchResponse, HTTPMethodRange, APICore as default };
47
+ export { APICore as default };
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  prepareAuth,
7
7
  prepareParams,
8
8
  prepareServer
9
- } from "./chunk-ASE5F2OQ.js";
9
+ } from "./chunk-J3R5ZB7L.js";
10
10
 
11
11
  // src/index.ts
12
12
  import oasToHar from "@readme/oas-to-har";
@@ -19,10 +19,8 @@ var APICore = class {
19
19
  config = {};
20
20
  userAgent;
21
21
  constructor(definition, userAgent) {
22
- if (definition)
23
- this.spec = Oas.init(definition);
24
- if (userAgent)
25
- this.userAgent = userAgent;
22
+ if (definition) this.spec = Oas.init(definition);
23
+ if (userAgent) this.userAgent = userAgent;
26
24
  }
27
25
  setSpec(spec) {
28
26
  this.spec = spec;
@@ -47,6 +45,14 @@ var APICore = class {
47
45
  const operation = this.spec.operation(path, method);
48
46
  return this.fetchOperation(operation, body, metadata);
49
47
  }
48
+ /**
49
+ * Retrieve a HAR for a given HTTP request.
50
+ *
51
+ * @internal
52
+ */
53
+ getHARForRequest(operation, data, auth) {
54
+ return oasToHar(this.spec, operation, data, auth);
55
+ }
50
56
  async fetchOperation(operation, body, metadata) {
51
57
  return prepareParams(operation, body, metadata).then((params) => {
52
58
  const data = { ...params };
@@ -56,7 +62,7 @@ var APICore = class {
56
62
  data.server = preparedServer;
57
63
  }
58
64
  }
59
- const har = oasToHar(this.spec, operation, data, prepareAuth(this.auth, operation));
65
+ const har = this.getHARForRequest(operation, data, prepareAuth(this.auth, operation));
60
66
  let timeoutSignal;
61
67
  const init = {};
62
68
  if (this.config.timeout) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Har } from 'har-format';\nimport type Operation from 'oas/operation';\nimport type { HttpMethods, OASDocument } from 'oas/rmoas.types';\n\nimport oasToHar from '@readme/oas-to-har';\nimport fetchHar from 'fetch-har';\nimport Oas from 'oas';\n\nimport FetchError from './errors/fetchError.js';\nimport { parseResponse, prepareAuth, prepareParams, prepareServer } from './lib/index.js';\n\nexport interface ConfigOptions {\n /**\n * Override the default `fetch` request timeout of 30 seconds. This number should be represented\n * in milliseconds.\n */\n timeout?: number;\n}\n\nexport interface FetchResponse<HTTPStatus, Data> {\n data: Data;\n headers: Headers;\n res: Response;\n status: HTTPStatus;\n}\n\n// https://stackoverflow.com/a/39495173\ntype Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N\n ? Acc[number]\n : Enumerate<N, [...Acc, Acc['length']]>;\n\nexport type HTTPMethodRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;\n\nexport default class APICore {\n spec!: Oas;\n\n private auth: (number | string)[] = [];\n\n private server:\n | false\n | {\n url: string;\n variables?: Record<string, string | number>;\n } = false;\n\n private config: ConfigOptions = {};\n\n private userAgent!: string;\n\n constructor(definition?: Record<string, unknown> | OASDocument, userAgent?: string) {\n if (definition) this.spec = Oas.init(definition);\n if (userAgent) this.userAgent = userAgent;\n }\n\n setSpec(spec: Oas) {\n this.spec = spec;\n }\n\n setConfig(config: ConfigOptions) {\n this.config = config;\n return this;\n }\n\n setUserAgent(userAgent: string) {\n this.userAgent = userAgent;\n return this;\n }\n\n setAuth(...values: string[] | number[]) {\n this.auth = values;\n return this;\n }\n\n setServer(url: string, variables: Record<string, string | number> = {}) {\n this.server = { url, variables };\n return this;\n }\n\n async fetch<HTTPStatus extends number = number>(\n path: string,\n method: HttpMethods,\n body?: unknown,\n metadata?: Record<string, unknown>,\n ) {\n const operation = this.spec.operation(path, method);\n\n return this.fetchOperation<HTTPStatus>(operation, body, metadata);\n }\n\n async fetchOperation<HTTPStatus extends number = number>(\n operation: Operation,\n body?: unknown,\n metadata?: Record<string, unknown>,\n ) {\n return prepareParams(operation, body, metadata).then(params => {\n const data = { ...params };\n\n // If `sdk.server()` has been issued data then we need to do some extra work to figure out\n // how to use that supplied server, and also handle any server variables that were sent\n // alongside it.\n if (this.server) {\n const preparedServer = prepareServer(this.spec, this.server.url, this.server.variables);\n if (preparedServer) {\n data.server = preparedServer;\n }\n }\n\n // @ts-expect-error `this.auth` typing is off. FIXME\n const har = oasToHar(this.spec, operation, data, prepareAuth(this.auth, operation));\n\n let timeoutSignal: NodeJS.Timeout;\n const init: RequestInit = {};\n if (this.config.timeout) {\n const controller = new AbortController();\n timeoutSignal = setTimeout(() => controller.abort(), this.config.timeout);\n init.signal = controller.signal;\n }\n\n return fetchHar(har as Har, {\n files: data.files || {},\n init,\n userAgent: this.userAgent,\n })\n .then(async (res: Response) => {\n const parsed = await parseResponse<HTTPStatus>(res);\n\n if (res.status >= 400 && res.status <= 599) {\n throw new FetchError<typeof parsed.status, typeof parsed.data>(\n parsed.status,\n parsed.data,\n parsed.headers,\n parsed.res,\n );\n }\n\n return parsed;\n })\n .finally(() => {\n if (this.config.timeout) {\n clearTimeout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ElF,UAAI;AACJ,YAAM,OAAoB,CAAC;AAC3B,UAAI,KAAK,OAAO,SAAS;AACvB,cAAM,aAAa,IAAI,gBAAgB;AACvC,wBAAgB,WAAW,MAAM,WAAW,MAAM,GAAG,KAAK,OAAO,OAAO;AACxE,aAAK,SAAS,WAAW;AAAA,MAC3B;AAEA,aAAO,SAAS,KAAY;AAAA,QAC1B,OAAO,KAAK,SAAS,CAAC;AAAA,QACtB;AAAA,QACA,WAAW,KAAK;AAAA,MAClB,CAAC,EACE,KAAK,OAAO,QAAkB;AAC7B,cAAM,SAAS,MAAM,cAA0B,GAAG;AAElD,YAAI,IAAI,UAAU,OAAO,IAAI,UAAU,KAAK;AAC1C,gBAAM,IAAI;AAAA,YACR,OAAO;AAAA,YACP,OAAO;AAAA,YACP,OAAO;AAAA,YACP,OAAO;AAAA,UACT;AAAA,QACF;AAEA,eAAO;AAAA,MACT,CAAC,EACA,QAAQ,MAAM;AACb,YAAI,KAAK,OAAO,SAAS;AACvB,uBAAa,aAAa;AAAA,QAC5B;AAAA,MACF,CAAC;AAAA,IACL,CAAC;AAAA,EACH;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { ConfigOptions } from './types.js';\nimport type { AuthForHAR, DataForHAR } from '@readme/oas-to-har/lib/types';\nimport type { Har } from 'har-format';\nimport type { Operation } from 'oas/operation';\nimport type { HttpMethods, OASDocument } from 'oas/types';\n\nimport oasToHar from '@readme/oas-to-har';\nimport fetchHar from 'fetch-har';\nimport Oas from 'oas';\n\nimport FetchError from './errors/fetchError.js';\nimport { parseResponse, prepareAuth, prepareParams, prepareServer } from './lib/index.js';\n\nexport default class APICore {\n spec!: Oas;\n\n private auth: (number | string)[] = [];\n\n private server:\n | false\n | {\n url: string;\n variables?: Record<string, number | string>;\n } = false;\n\n private config: ConfigOptions = {};\n\n private userAgent!: string;\n\n constructor(definition?: OASDocument | Record<string, unknown>, userAgent?: string) {\n if (definition) this.spec = Oas.init(definition);\n if (userAgent) this.userAgent = userAgent;\n }\n\n setSpec(spec: Oas) {\n this.spec = spec;\n }\n\n setConfig(config: ConfigOptions) {\n this.config = config;\n return this;\n }\n\n setUserAgent(userAgent: string) {\n this.userAgent = userAgent;\n return this;\n }\n\n setAuth(...values: number[] | string[]) {\n this.auth = values;\n return this;\n }\n\n setServer(url: string, variables: Record<string, number | string> = {}) {\n this.server = { url, variables };\n return this;\n }\n\n async fetch<HTTPStatus extends number = number>(\n path: string,\n method: HttpMethods,\n body?: unknown,\n metadata?: Record<string, unknown>,\n ) {\n const operation = this.spec.operation(path, method);\n\n return this.fetchOperation<HTTPStatus>(operation, body, metadata);\n }\n\n /**\n * Retrieve a HAR for a given HTTP request.\n *\n * @internal\n */\n getHARForRequest(operation: Operation, data: DataForHAR, auth: AuthForHAR) {\n return oasToHar(this.spec, operation, data, auth);\n }\n\n async fetchOperation<HTTPStatus extends number = number>(\n operation: Operation,\n body?: unknown,\n metadata?: Record<string, unknown>,\n ) {\n return prepareParams(operation, body, metadata).then(params => {\n const data = { ...params };\n\n // If `sdk.server()` has been issued data then we need to do some extra work to figure out\n // how to use that supplied server, and also handle any server variables that were sent\n // alongside it.\n if (this.server) {\n const preparedServer = prepareServer(this.spec, this.server.url, this.server.variables);\n if (preparedServer) {\n data.server = preparedServer;\n }\n }\n\n const har = this.getHARForRequest(operation, data, prepareAuth(this.auth, operation));\n\n let timeoutSignal: NodeJS.Timeout;\n const init: RequestInit = {};\n if (this.config.timeout) {\n const controller = new AbortController();\n timeoutSignal = setTimeout(() => controller.abort(), this.config.timeout);\n init.signal = controller.signal;\n }\n\n return fetchHar(har as Har, {\n files: data.files || {},\n init,\n userAgent: this.userAgent,\n })\n .then(async (res: Response) => {\n const parsed = await parseResponse<HTTPStatus>(res);\n\n if (res.status >= 400 && res.status <= 599) {\n throw new FetchError<typeof parsed.status, typeof parsed.data>(\n parsed.status,\n parsed.data,\n parsed.headers,\n parsed.res,\n );\n }\n\n return parsed;\n })\n .finally(() => {\n if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AAO,SAAS;AACtF,YAAI,gBAAgB;AAClB,eAAK,SAAS;AAAA,QAChB;AAAA,MACF;AAEA,YAAM,MAAM,KAAK,iBAAiB,WAAW,MAAM,YAAY,KAAK,MAAM,SAAS,CAAC;AAEpF,UAAI;AACJ,YAAM,OAAoB,CAAC;AAC3B,UAAI,KAAK,OAAO,SAAS;AACvB,cAAM,aAAa,IAAI,gBAAgB;AACvC,wBAAgB,WAAW,MAAM,WAAW,MAAM,GAAG,KAAK,OAAO,OAAO;AACxE,aAAK,SAAS,WAAW;AAAA,MAC3B;AAEA,aAAO,SAAS,KAAY;AAAA,QAC1B,OAAO,KAAK,SAAS,CAAC;AAAA,QACtB;AAAA,QACA,WAAW,KAAK;AAAA,MAClB,CAAC,EACE,KAAK,OAAO,QAAkB;AAC7B,cAAM,SAAS,MAAM,cAA0B,GAAG;AAElD,YAAI,IAAI,UAAU,OAAO,IAAI,UAAU,KAAK;AAC1C,gBAAM,IAAI;AAAA,YACR,OAAO;AAAA,YACP,OAAO;AAAA,YACP,OAAO;AAAA,YACP,OAAO;AAAA,UACT;AAAA,QACF;AAEA,eAAO;AAAA,MACT,CAAC,EACA,QAAQ,MAAM;AACb,YAAI,KAAK,OAAO,SAAS;AACvB,uBAAa,aAAa;AAAA,QAC5B;AAAA,MACF,CAAC;AAAA,IACL,CAAC;AAAA,EACH;AACF;","names":[]}
@@ -4,12 +4,12 @@
4
4
 
5
5
 
6
6
 
7
- var _chunkHGLVXDVZcjs = require('../chunk-HGLVXDVZ.cjs');
7
+ var _chunkHA453OBWcjs = require('../chunk-HA453OBW.cjs');
8
8
 
9
9
 
10
10
 
11
11
 
12
12
 
13
13
 
14
- exports.getJSONSchemaDefaults = _chunkHGLVXDVZcjs.getJSONSchemaDefaults; exports.parseResponse = _chunkHGLVXDVZcjs.parseResponse; exports.prepareAuth = _chunkHGLVXDVZcjs.prepareAuth; exports.prepareParams = _chunkHGLVXDVZcjs.prepareParams; exports.prepareServer = _chunkHGLVXDVZcjs.prepareServer;
14
+ exports.getJSONSchemaDefaults = _chunkHA453OBWcjs.getJSONSchemaDefaults; exports.parseResponse = _chunkHA453OBWcjs.parseResponse; exports.prepareAuth = _chunkHA453OBWcjs.prepareAuth; exports.prepareParams = _chunkHA453OBWcjs.prepareParams; exports.prepareServer = _chunkHA453OBWcjs.prepareServer;
15
15
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
1
+ {"version":3,"sources":["/Users/kanadg/Code/readmeio/api/packages/core/dist/lib/index.cjs"],"names":[],"mappings":"AAAA;AACE;AACA;AACA;AACA;AACA;AACF,yDAA8B;AAC9B;AACE;AACA;AACA;AACA;AACA;AACF,wSAAC","file":"/Users/kanadg/Code/readmeio/api/packages/core/dist/lib/index.cjs"}
@@ -1,6 +1,6 @@
1
- export { FromSchema } from 'json-schema-to-ts';
2
1
  import { SchemaWrapper } from 'oas/operation/get-parameters-as-json-schema';
3
- import Operation from 'oas/operation';
2
+ import { AuthForHAR } from '@readme/oas-to-har/lib/types';
3
+ import { Operation } from 'oas/operation';
4
4
  import Oas from 'oas';
5
5
 
6
6
  /**
@@ -24,10 +24,7 @@ declare function parseResponse<HTTPStatus extends number = number>(response: Res
24
24
  res: Response;
25
25
  }>;
26
26
 
27
- declare function prepareAuth(authKey: (number | string)[], operation: Operation): Record<string, string | number | {
28
- pass: string | number;
29
- user: string | number;
30
- }>;
27
+ declare function prepareAuth(authKey: (number | string)[], operation: Operation): AuthForHAR;
31
28
 
32
29
  /**
33
30
  * With potentially supplied body and/or metadata we need to run through them against a given API
@@ -37,16 +34,16 @@ declare function prepareAuth(authKey: (number | string)[], operation: Operation)
37
34
  */
38
35
  declare function prepareParams(operation: Operation, body?: unknown, metadata?: Record<string, unknown>): Promise<{
39
36
  body?: any;
40
- cookie?: Record<string, string | number | boolean> | undefined;
41
- files?: Record<string, Buffer> | undefined;
37
+ cookie?: Record<string, boolean | number | string>;
38
+ files?: Record<string, Buffer>;
42
39
  formData?: any;
43
- header?: Record<string, string | number | boolean> | undefined;
44
- path?: Record<string, string | number | boolean> | undefined;
45
- query?: Record<string, string | number | boolean> | undefined;
40
+ header?: Record<string, boolean | number | string>;
41
+ path?: Record<string, boolean | number | string>;
42
+ query?: Record<string, boolean | number | string>;
46
43
  server?: {
47
44
  selected: number;
48
- variables: Record<string, string | number>;
49
- } | undefined;
45
+ variables: Record<string, number | string>;
46
+ };
50
47
  }>;
51
48
 
52
49
  /**
@@ -54,7 +51,7 @@ declare function prepareParams(operation: Operation, body?: unknown, metadata?:
54
51
  * any server variables to be supplied to `@readme/oas-to-har`.
55
52
  *
56
53
  */
57
- declare function prepareServer(spec: Oas, url: string, variables?: Record<string, string | number>): false | {
54
+ declare function prepareServer(spec: Oas, url: string, variables?: Record<string, number | string>): false | {
58
55
  selected: number;
59
56
  variables: Record<string, string | number>;
60
57
  };
@@ -1,6 +1,6 @@
1
- export { FromSchema } from 'json-schema-to-ts';
2
1
  import { SchemaWrapper } from 'oas/operation/get-parameters-as-json-schema';
3
- import Operation from 'oas/operation';
2
+ import { AuthForHAR } from '@readme/oas-to-har/lib/types';
3
+ import { Operation } from 'oas/operation';
4
4
  import Oas from 'oas';
5
5
 
6
6
  /**
@@ -24,10 +24,7 @@ declare function parseResponse<HTTPStatus extends number = number>(response: Res
24
24
  res: Response;
25
25
  }>;
26
26
 
27
- declare function prepareAuth(authKey: (number | string)[], operation: Operation): Record<string, string | number | {
28
- pass: string | number;
29
- user: string | number;
30
- }>;
27
+ declare function prepareAuth(authKey: (number | string)[], operation: Operation): AuthForHAR;
31
28
 
32
29
  /**
33
30
  * With potentially supplied body and/or metadata we need to run through them against a given API
@@ -37,16 +34,16 @@ declare function prepareAuth(authKey: (number | string)[], operation: Operation)
37
34
  */
38
35
  declare function prepareParams(operation: Operation, body?: unknown, metadata?: Record<string, unknown>): Promise<{
39
36
  body?: any;
40
- cookie?: Record<string, string | number | boolean> | undefined;
41
- files?: Record<string, Buffer> | undefined;
37
+ cookie?: Record<string, boolean | number | string>;
38
+ files?: Record<string, Buffer>;
42
39
  formData?: any;
43
- header?: Record<string, string | number | boolean> | undefined;
44
- path?: Record<string, string | number | boolean> | undefined;
45
- query?: Record<string, string | number | boolean> | undefined;
40
+ header?: Record<string, boolean | number | string>;
41
+ path?: Record<string, boolean | number | string>;
42
+ query?: Record<string, boolean | number | string>;
46
43
  server?: {
47
44
  selected: number;
48
- variables: Record<string, string | number>;
49
- } | undefined;
45
+ variables: Record<string, number | string>;
46
+ };
50
47
  }>;
51
48
 
52
49
  /**
@@ -54,7 +51,7 @@ declare function prepareParams(operation: Operation, body?: unknown, metadata?:
54
51
  * any server variables to be supplied to `@readme/oas-to-har`.
55
52
  *
56
53
  */
57
- declare function prepareServer(spec: Oas, url: string, variables?: Record<string, string | number>): false | {
54
+ declare function prepareServer(spec: Oas, url: string, variables?: Record<string, number | string>): false | {
58
55
  selected: number;
59
56
  variables: Record<string, string | number>;
60
57
  };
package/dist/lib/index.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  prepareAuth,
5
5
  prepareParams,
6
6
  prepareServer
7
- } from "../chunk-ASE5F2OQ.js";
7
+ } from "../chunk-J3R5ZB7L.js";
8
8
  export {
9
9
  getJSONSchemaDefaults,
10
10
  parseResponse,
package/dist/types.cjs ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/types.ts
2
+ var _jsonschematots = require('json-schema-to-ts');
3
+
4
+
5
+ exports.FromSchema = _jsonschematots.FromSchema;
6
+ //# sourceMappingURL=types.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/Users/kanadg/Code/readmeio/api/packages/core/dist/types.cjs","../src/types.ts"],"names":[],"mappings":"AAAA;ACAA,mDAA2B;ADE3B;AACE;AACF,gDAAC","file":"/Users/kanadg/Code/readmeio/api/packages/core/dist/types.cjs","sourcesContent":[null,"export { FromSchema } from 'json-schema-to-ts';\n\nexport interface ConfigOptions {\n /**\n * Override the default `fetch` request timeout of 30 seconds. This number should be represented\n * in milliseconds.\n */\n timeout?: number;\n}\n\n/**\n * @see {@link https://stackoverflow.com/a/39495173}\n */\ntype Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N\n ? Acc[number]\n : Enumerate<N, [...Acc, Acc['length']]>;\n\nexport type HTTPMethodRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;\n\nexport interface FetchResponse<HTTPStatus, Data> {\n data: Data;\n headers: Headers;\n res: Response;\n status: HTTPStatus;\n}\n"]}
@@ -0,0 +1,22 @@
1
+ export { FromSchema } from 'json-schema-to-ts';
2
+
3
+ interface ConfigOptions {
4
+ /**
5
+ * Override the default `fetch` request timeout of 30 seconds. This number should be represented
6
+ * in milliseconds.
7
+ */
8
+ timeout?: number;
9
+ }
10
+ /**
11
+ * @see {@link https://stackoverflow.com/a/39495173}
12
+ */
13
+ type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc['length']]>;
14
+ type HTTPMethodRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;
15
+ interface FetchResponse<HTTPStatus, Data> {
16
+ data: Data;
17
+ headers: Headers;
18
+ res: Response;
19
+ status: HTTPStatus;
20
+ }
21
+
22
+ export type { ConfigOptions, FetchResponse, HTTPMethodRange };
@@ -0,0 +1,22 @@
1
+ export { FromSchema } from 'json-schema-to-ts';
2
+
3
+ interface ConfigOptions {
4
+ /**
5
+ * Override the default `fetch` request timeout of 30 seconds. This number should be represented
6
+ * in milliseconds.
7
+ */
8
+ timeout?: number;
9
+ }
10
+ /**
11
+ * @see {@link https://stackoverflow.com/a/39495173}
12
+ */
13
+ type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc['length']]>;
14
+ type HTTPMethodRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;
15
+ interface FetchResponse<HTTPStatus, Data> {
16
+ data: Data;
17
+ headers: Headers;
18
+ res: Response;
19
+ status: HTTPStatus;
20
+ }
21
+
22
+ export type { ConfigOptions, FetchResponse, HTTPMethodRange };
package/dist/types.js ADDED
@@ -0,0 +1,6 @@
1
+ // src/types.ts
2
+ import { FromSchema } from "json-schema-to-ts";
3
+ export {
4
+ FromSchema
5
+ };
6
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/types.ts"],"sourcesContent":["export { FromSchema } from 'json-schema-to-ts';\n\nexport interface ConfigOptions {\n /**\n * Override the default `fetch` request timeout of 30 seconds. This number should be represented\n * in milliseconds.\n */\n timeout?: number;\n}\n\n/**\n * @see {@link https://stackoverflow.com/a/39495173}\n */\ntype Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N\n ? Acc[number]\n : Enumerate<N, [...Acc, Acc['length']]>;\n\nexport type HTTPMethodRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;\n\nexport interface FetchResponse<HTTPStatus, Data> {\n data: Data;\n headers: Headers;\n res: Response;\n status: HTTPStatus;\n}\n"],"mappings":";AAAA,SAAS,kBAAkB;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@readme/api-core",
3
- "version": "7.0.0-beta.1",
3
+ "version": "7.0.0-beta.10",
4
4
  "description": "The magic behind `api` 🧙",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -17,6 +17,10 @@
17
17
  "require": "./dist/lib/index.cjs",
18
18
  "import": "./dist/lib/index.js"
19
19
  },
20
+ "./types": {
21
+ "require": "./dist/types.d.cjs",
22
+ "import": "./dist/types.d.js"
23
+ },
20
24
  "./package.json": "./package.json"
21
25
  },
22
26
  "main": "dist/index.cjs",
@@ -45,27 +49,27 @@
45
49
  "node": ">=18"
46
50
  },
47
51
  "dependencies": {
48
- "@readme/oas-to-har": "^23.0.12",
52
+ "@readme/oas-to-har": "^24.0.0",
49
53
  "caseless": "^0.12.0",
50
54
  "datauri": "^4.1.0",
51
55
  "fetch-har": "^11.0.1",
52
- "json-schema-to-ts": "^2.9.2",
56
+ "json-schema-to-ts": "^3.0.0",
53
57
  "json-schema-traverse": "^1.0.0",
54
58
  "lodash.merge": "^4.6.2",
55
- "oas": "^23.0.0",
59
+ "oas": "^25.0.1",
56
60
  "remove-undefined-objects": "^5.0.0"
57
61
  },
58
62
  "devDependencies": {
59
- "@api/test-utils": "^7.0.0-beta.1",
63
+ "@api/test-utils": "^7.0.0-beta.10",
60
64
  "@readme/oas-examples": "^5.12.0",
61
- "@types/caseless": "^0.12.3",
62
- "@types/lodash.merge": "^4.6.7",
63
- "@vitest/coverage-v8": "^0.34.4",
64
- "fetch-mock": "^9.11.0",
65
- "get-stream": "^8.0.1",
65
+ "@types/caseless": "^0.12.5",
66
+ "@types/lodash.merge": "^4.6.9",
67
+ "@vitest/coverage-v8": "^2.1.3",
68
+ "fetch-mock": "^11.1.3",
69
+ "get-stream": "^9.0.1",
66
70
  "typescript": "^5.2.2",
67
- "vitest": "^0.34.5"
71
+ "vitest": "^2.1.3"
68
72
  },
69
73
  "prettier": "@readme/eslint-config/prettier",
70
- "gitHead": "e31539d88b5af53ed7be05495329de1c2a70c93f"
74
+ "gitHead": "4a34d5d4642a94928bd4c299d8c45c6b64ee6f20"
71
75
  }
package/src/index.ts CHANGED
@@ -1,6 +1,8 @@
1
+ import type { ConfigOptions } from './types.js';
2
+ import type { AuthForHAR, DataForHAR } from '@readme/oas-to-har/lib/types';
1
3
  import type { Har } from 'har-format';
2
- import type Operation from 'oas/operation';
3
- import type { HttpMethods, OASDocument } from 'oas/rmoas.types';
4
+ import type { Operation } from 'oas/operation';
5
+ import type { HttpMethods, OASDocument } from 'oas/types';
4
6
 
5
7
  import oasToHar from '@readme/oas-to-har';
6
8
  import fetchHar from 'fetch-har';
@@ -9,28 +11,6 @@ import Oas from 'oas';
9
11
  import FetchError from './errors/fetchError.js';
10
12
  import { parseResponse, prepareAuth, prepareParams, prepareServer } from './lib/index.js';
11
13
 
12
- export interface ConfigOptions {
13
- /**
14
- * Override the default `fetch` request timeout of 30 seconds. This number should be represented
15
- * in milliseconds.
16
- */
17
- timeout?: number;
18
- }
19
-
20
- export interface FetchResponse<HTTPStatus, Data> {
21
- data: Data;
22
- headers: Headers;
23
- res: Response;
24
- status: HTTPStatus;
25
- }
26
-
27
- // https://stackoverflow.com/a/39495173
28
- type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N
29
- ? Acc[number]
30
- : Enumerate<N, [...Acc, Acc['length']]>;
31
-
32
- export type HTTPMethodRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;
33
-
34
14
  export default class APICore {
35
15
  spec!: Oas;
36
16
 
@@ -40,14 +20,14 @@ export default class APICore {
40
20
  | false
41
21
  | {
42
22
  url: string;
43
- variables?: Record<string, string | number>;
23
+ variables?: Record<string, number | string>;
44
24
  } = false;
45
25
 
46
26
  private config: ConfigOptions = {};
47
27
 
48
28
  private userAgent!: string;
49
29
 
50
- constructor(definition?: Record<string, unknown> | OASDocument, userAgent?: string) {
30
+ constructor(definition?: OASDocument | Record<string, unknown>, userAgent?: string) {
51
31
  if (definition) this.spec = Oas.init(definition);
52
32
  if (userAgent) this.userAgent = userAgent;
53
33
  }
@@ -66,12 +46,12 @@ export default class APICore {
66
46
  return this;
67
47
  }
68
48
 
69
- setAuth(...values: string[] | number[]) {
49
+ setAuth(...values: number[] | string[]) {
70
50
  this.auth = values;
71
51
  return this;
72
52
  }
73
53
 
74
- setServer(url: string, variables: Record<string, string | number> = {}) {
54
+ setServer(url: string, variables: Record<string, number | string> = {}) {
75
55
  this.server = { url, variables };
76
56
  return this;
77
57
  }
@@ -87,6 +67,15 @@ export default class APICore {
87
67
  return this.fetchOperation<HTTPStatus>(operation, body, metadata);
88
68
  }
89
69
 
70
+ /**
71
+ * Retrieve a HAR for a given HTTP request.
72
+ *
73
+ * @internal
74
+ */
75
+ getHARForRequest(operation: Operation, data: DataForHAR, auth: AuthForHAR) {
76
+ return oasToHar(this.spec, operation, data, auth);
77
+ }
78
+
90
79
  async fetchOperation<HTTPStatus extends number = number>(
91
80
  operation: Operation,
92
81
  body?: unknown,
@@ -105,8 +94,7 @@ export default class APICore {
105
94
  }
106
95
  }
107
96
 
108
- // @ts-expect-error `this.auth` typing is off. FIXME
109
- const har = oasToHar(this.spec, operation, data, prepareAuth(this.auth, operation));
97
+ const har = this.getHARForRequest(operation, data, prepareAuth(this.auth, operation));
110
98
 
111
99
  let timeoutSignal: NodeJS.Timeout;
112
100
  const init: RequestInit = {};
@@ -1,5 +1,5 @@
1
1
  import type { SchemaWrapper } from 'oas/operation/get-parameters-as-json-schema';
2
- import type { SchemaObject } from 'oas/rmoas.types';
2
+ import type { SchemaObject } from 'oas/types';
3
3
 
4
4
  import traverse from 'json-schema-traverse';
5
5
 
@@ -26,7 +26,7 @@ export default function getJSONSchemaDefaults(jsonSchemas: SchemaWrapper[]) {
26
26
  parentPointer?: string,
27
27
  parentKeyword?: string,
28
28
  parentSchema?: SchemaObject,
29
- indexProperty?: string | number,
29
+ indexProperty?: number | string,
30
30
  ) => {
31
31
  if (!pointer.startsWith('/properties/')) {
32
32
  return;
package/src/lib/index.ts CHANGED
@@ -1,9 +1,7 @@
1
- import type { FromSchema } from 'json-schema-to-ts';
2
-
3
1
  import getJSONSchemaDefaults from './getJSONSchemaDefaults.js';
4
2
  import parseResponse from './parseResponse.js';
5
3
  import prepareAuth from './prepareAuth.js';
6
4
  import prepareParams from './prepareParams.js';
7
5
  import prepareServer from './prepareServer.js';
8
6
 
9
- export { FromSchema, getJSONSchemaDefaults, parseResponse, prepareAuth, prepareParams, prepareServer };
7
+ export { getJSONSchemaDefaults, parseResponse, prepareAuth, prepareParams, prepareServer };
@@ -4,13 +4,14 @@ export default async function parseResponse<HTTPStatus extends number = number>(
4
4
  const contentType = response.headers.get('Content-Type');
5
5
  const isJSON = contentType && (matchesMimeType.json(contentType) || matchesMimeType.wildcard(contentType));
6
6
 
7
- const responseBody = await response.text();
7
+ const responseBody = await response.clone().text();
8
8
 
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
10
  let data: any = responseBody;
11
11
  if (isJSON) {
12
12
  try {
13
13
  data = JSON.parse(responseBody);
14
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
14
15
  } catch (e) {
15
16
  // If our JSON parsing failed then we can just return plaintext instead.
16
17
  }