api 3.4.1 → 4.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api",
3
- "version": "3.4.1",
3
+ "version": "4.1.1",
4
4
  "description": "Generate an SDK from an OpenAPI definition",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -23,9 +23,8 @@
23
23
  "node": "^12 || ^14 || ^16"
24
24
  },
25
25
  "dependencies": {
26
- "@apidevtools/json-schema-ref-parser": "^9.0.1",
27
- "@apidevtools/swagger-parser": "^10.0.1",
28
- "@readme/oas-to-har": "^13.7.2",
26
+ "@readme/oas-to-har": "^14.0.0",
27
+ "@readme/openapi-parser": "^1.1.0",
29
28
  "datauri": "^4.1.0",
30
29
  "fetch-har": "^5.0.0",
31
30
  "find-cache-dir": "^3.3.1",
@@ -35,16 +34,16 @@
35
34
  "make-dir": "^3.1.0",
36
35
  "mimer": "^2.0.2",
37
36
  "node-fetch": "^2.6.0",
38
- "oas": "^14.5.1"
37
+ "oas": "^17.1.0"
39
38
  },
40
39
  "devDependencies": {
41
- "@readme/eslint-config": "^7.2.2",
42
- "@readme/oas-examples": "^4.2.0",
43
- "eslint": "^7.32.0",
44
- "jest": "^27.2.0",
40
+ "@readme/eslint-config": "^8.0.2",
41
+ "@readme/oas-examples": "^4.3.2",
42
+ "eslint": "^8.3.0",
43
+ "jest": "^27.3.1",
45
44
  "memfs": "^3.2.4",
46
45
  "nock": "^13.1.3",
47
- "prettier": "^2.4.0"
46
+ "prettier": "^2.4.1"
48
47
  },
49
48
  "prettier": "@readme/eslint-config/prettier",
50
49
  "jest": {
@@ -52,5 +51,5 @@
52
51
  "__tests__/__fixtures__/"
53
52
  ]
54
53
  },
55
- "gitHead": "ebed0fcc41452f693ae46926005a77c29f382d1d"
54
+ "gitHead": "07317288fb424c9bfbeb61f64a29a77a740eb8c3"
56
55
  }
package/src/cache.js CHANGED
@@ -1,6 +1,5 @@
1
1
  const fetch = require('node-fetch');
2
- const SwaggerParser = require('@apidevtools/swagger-parser');
3
- const $RefParser = require('@apidevtools/json-schema-ref-parser');
2
+ const OpenAPIParser = require('@readme/openapi-parser');
4
3
  const yaml = require('js-yaml');
5
4
  const crypto = require('crypto');
6
5
  const findCacheDir = require('find-cache-dir');
@@ -124,20 +123,25 @@ class SdkCache {
124
123
  }
125
124
 
126
125
  return new Promise(resolve => {
127
- return resolve(json);
126
+ resolve(json);
128
127
  })
129
128
  .then(res => {
130
- return SwaggerParser.validate(res).catch(err => {
131
- if (/is not a valid openapi api definition/i.test(err.message)) {
129
+ // The `validate` method handles dereferencing for us.
130
+ return OpenAPIParser.validate(res, {
131
+ dereference: {
132
+ // If circular `$refs` are ignored they'll remain in the API definition as `$ref: String`. This allows us to
133
+ // not only do easy circular reference detection but also stringify and save dereferenced API definitions
134
+ // back into the cache directory.
135
+ circular: 'ignore',
136
+ },
137
+ }).catch(err => {
138
+ if (/is not a valid openapi definition/i.test(err.message)) {
132
139
  throw new Error("Sorry, that doesn't look like a valid OpenAPI definition.");
133
140
  }
134
141
 
135
142
  throw err;
136
143
  });
137
144
  })
138
- .then(res => {
139
- return $RefParser.dereference(res);
140
- })
141
145
  .then(async spec => {
142
146
  if (!fs.existsSync(self.dir)) {
143
147
  fs.mkdirSync(self.dir, { recursive: true });
@@ -189,7 +193,7 @@ class SdkCache {
189
193
 
190
194
  saveFile() {
191
195
  return new Promise(resolve => {
192
- return resolve(fs.readFileSync(this.uri, 'utf8'));
196
+ resolve(fs.readFileSync(this.uri, 'utf8'));
193
197
  })
194
198
  .then(res => {
195
199
  if (/\.(yaml|yml)/.test(this.uri)) {
package/src/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  const fetch = require('node-fetch');
2
2
  const fetchHar = require('fetch-har');
3
- const Oas = require('oas');
3
+ const Oas = require('oas').default;
4
4
  const oasToHar = require('@readme/oas-to-har');
5
5
  const pkg = require('../package.json');
6
6
 
7
7
  const Cache = require('./cache');
8
- const { parseResponse, prepareAuth, prepareParams, prepareServer } = require('./lib/index');
8
+ const { parseResponse, prepareAuth, prepareParams, prepareServer } = require('./lib');
9
9
 
10
10
  global.fetch = fetch;
11
11
  global.Request = fetch.Request;
@@ -19,9 +19,9 @@ class Sdk {
19
19
  }
20
20
 
21
21
  static getOperations(spec) {
22
- return Object.keys(spec.paths)
22
+ return Object.keys(spec.api.paths)
23
23
  .map(path => {
24
- return Object.keys(spec.paths[path]).map(method => {
24
+ return Object.keys(spec.api.paths[path]).map(method => {
25
25
  return spec.operation(path, method);
26
26
  });
27
27
  })
@@ -1,6 +1,8 @@
1
1
  // Nabbed from here:
2
2
  // https://github.com/readmeio/api-explorer/blob/77b90ebed4673f168354cdcd730e34b7ee016360/packages/api-explorer/src/lib/parse-response.js#L13-L30
3
- const { matchesMimeType } = require('oas/src/utils');
3
+ const {
4
+ utils: { matchesMimeType },
5
+ } = require('oas');
4
6
 
5
7
  module.exports = async function getResponseBody(response) {
6
8
  const contentType = response.headers.get('Content-Type');
@@ -4,7 +4,9 @@ const stream = require('stream');
4
4
  const mimer = require('mimer');
5
5
  const getStream = require('get-stream');
6
6
  const datauri = require('datauri');
7
- const { getSchema } = require('oas/src/utils');
7
+ const {
8
+ utils: { getSchema },
9
+ } = require('oas');
8
10
 
9
11
  function digestParameters(parameters) {
10
12
  return parameters.reduce((prev, param) => {
@@ -87,7 +89,7 @@ module.exports = async (operation, body, metadata) => {
87
89
  // body payload to see if anything in there is either a file path or a file stream so we can translate those into a
88
90
  // data URL for `@readme/oas-to-har` to make a request.
89
91
  if ('body' in params && operation.isMultipart()) {
90
- let requestBody = getSchema(operation.schema, operation.oas);
92
+ let requestBody = getSchema(operation.schema, operation.api);
91
93
  if (requestBody) {
92
94
  requestBody = requestBody.schema;
93
95
  } else {
@@ -109,7 +111,9 @@ module.exports = async (operation, body, metadata) => {
109
111
  file = path.resolve(file);
110
112
  if (fs.existsSync(file)) {
111
113
  conversions.push(
112
- new Promise(resolve => resolve(datauri(file))).then(dataurl => {
114
+ new Promise(resolve => {
115
+ resolve(datauri(file));
116
+ }).then(dataurl => {
113
117
  // Doing this manually for now until when/if https://github.com/data-uri/datauri/pull/29 is accepted.
114
118
  params.body[prop] = dataurl.replace(
115
119
  ';base64',
@@ -122,7 +126,9 @@ module.exports = async (operation, body, metadata) => {
122
126
  }
123
127
  } else if (file instanceof stream.Readable) {
124
128
  conversions.push(
125
- new Promise(resolve => resolve(getStream.buffer(file))).then(buffer => {
129
+ new Promise(resolve => {
130
+ resolve(getStream.buffer(file));
131
+ }).then(buffer => {
126
132
  // This logic was taken from the `datauri` package, and ideally it should be able to accept the content
127
133
  // of a file, or a file stream, but I'll PR that later to that package.
128
134
  // @todo
@@ -18,7 +18,7 @@ function stripTrailingSlash(url) {
18
18
  module.exports = (spec, url, variables = {}) => {
19
19
  let serverIdx;
20
20
  const sanitizedUrl = stripTrailingSlash(url);
21
- (spec.servers || []).forEach((server, i) => {
21
+ (spec.api.servers || []).forEach((server, i) => {
22
22
  if (server.url === sanitizedUrl) {
23
23
  serverIdx = i;
24
24
  }