api 4.4.0 → 4.5.0

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/README.md CHANGED
@@ -23,7 +23,7 @@ npm install api --save
23
23
  Using `api` is as simple as supplying it an OpenAPI and using the SDK as you would any other!
24
24
 
25
25
  ```js
26
- const sdk = require('api')('https://raw.githubusercontent.com/readmeio/oas/master/packages/examples/3.0/json/petstore.json');
26
+ const sdk = require('api')('https://raw.githubusercontent.com/readmeio/oas-examples/main/3.0/json/petstore.json');
27
27
 
28
28
  sdk.listPets().then(res => {
29
29
  console.log(`My pets name is ${res[0].name}!`);
@@ -164,3 +164,17 @@ By default we parse the response based on the `content-type` header for you. You
164
164
  ```js
165
165
  sdk.config({ parseResponse: false });
166
166
  ```
167
+
168
+ #### Where is the cache stored?
169
+
170
+ By default the cache is configured with the [find-cache-dir](https://npm.im/find-cache-dir) library so the cache will be in `node_modules/.cache/api`. If placing this cache within the `node_modules/` directory is a problem for your environment (maybe you use `npm prune`) you can configure this by supplying an additional argument to the SDK instantiator:
171
+
172
+ ```js
173
+ const sdk = require('api')('https://raw.githubusercontent.com/readmeio/oas-examples/main/3.0/json/petstore.json', {
174
+ cacheDir: './path/to/my/custom/cache/dir',
175
+ });
176
+
177
+ sdk.listPets().then(res => {
178
+ console.log(`My pets name is ${res[0].name}!`);
179
+ });
180
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api",
3
- "version": "4.4.0",
3
+ "version": "4.5.0",
4
4
  "description": "Generate an SDK from an OpenAPI definition",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -48,8 +48,9 @@
48
48
  "prettier": "@readme/eslint-config/prettier",
49
49
  "jest": {
50
50
  "testPathIgnorePatterns": [
51
- "__tests__/__fixtures__/"
51
+ "__tests__/__fixtures__/",
52
+ ".api-test/"
52
53
  ]
53
54
  },
54
- "gitHead": "dab29250c2aea8ff2148aa35d50f9fc11cf69647"
55
+ "gitHead": "2cbe023cb5d2c1ff1f8b46a4c90d49bb7af0c5f1"
55
56
  }
package/src/cache.js CHANGED
@@ -9,18 +9,8 @@ const os = require('os');
9
9
  const path = require('path');
10
10
  const makeDir = require('make-dir');
11
11
 
12
- let cacheDir = findCacheDir({ name: pkg.name });
13
- if (typeof cacheDir === 'undefined') {
14
- // The `find-cache-dir` module returns `undefined` if the `node_modules/` directory isn't writable, or there's no
15
- // `package.json` in the root-most directory. If this happens, we can instead adhoc create a cache directory in the
16
- // users OS temp directory and store our data there.
17
- //
18
- // @link https://github.com/avajs/find-cache-dir/issues/29
19
- cacheDir = makeDir.sync(path.join(os.tmpdir(), pkg.name));
20
- }
21
-
22
- class SdkCache {
23
- constructor(uri) {
12
+ class Cache {
13
+ constructor(uri, cacheDir = false) {
24
14
  /**
25
15
  * Resolve OpenAPI definition shorthand accessors from within the ReadMe API Registry.
26
16
  *
@@ -37,8 +27,23 @@ class SdkCache {
37
27
  : u;
38
28
 
39
29
  this.uri = resolveReadMeRegistryAccessor(uri);
40
- this.uriHash = SdkCache.getCacheHash(this.uri);
41
- this.dir = cacheDir;
30
+ this.uriHash = Cache.getCacheHash(this.uri);
31
+
32
+ if (cacheDir) {
33
+ this.dir = cacheDir;
34
+ } else {
35
+ this.dir = findCacheDir({ name: pkg.name });
36
+ if (typeof this.dir === 'undefined') {
37
+ // The `find-cache-dir` module returns `undefined` if the `node_modules/` directory isn't
38
+ // writable, or there's no `package.json` in the root-most directory. If this happens, we
39
+ // can instead adhoc create a cache directory in the users OS temp directory and store our
40
+ // data there.
41
+ //
42
+ // @link https://github.com/avajs/find-cache-dir/issues/29
43
+ this.dir = makeDir.sync(path.join(os.tmpdir(), pkg.name));
44
+ }
45
+ }
46
+
42
47
  this.cacheStore = path.join(this.dir, 'cache.json');
43
48
  this.specsCache = path.join(this.dir, 'specs');
44
49
 
@@ -206,4 +211,4 @@ class SdkCache {
206
211
  }
207
212
  }
208
213
 
209
- module.exports = SdkCache;
214
+ module.exports = Cache;
package/src/index.js CHANGED
@@ -13,9 +13,11 @@ global.Headers = fetch.Headers;
13
13
  global.FormData = require('form-data');
14
14
 
15
15
  class Sdk {
16
- constructor(uri) {
16
+ constructor(uri, opts = {}) {
17
17
  this.uri = uri;
18
18
  this.userAgent = `${pkg.name} (node)/${pkg.version}`;
19
+
20
+ this.cacheDir = opts.cacheDir ? opts.cacheDir : false;
19
21
  }
20
22
 
21
23
  static getOperations(spec) {
@@ -30,7 +32,7 @@ class Sdk {
30
32
 
31
33
  load() {
32
34
  let authKeys = [];
33
- const cache = new Cache(this.uri);
35
+ const cache = new Cache(this.uri, this.cacheDir);
34
36
  const self = this;
35
37
  let config = { parseResponse: true };
36
38
  let server = false;
@@ -170,6 +172,6 @@ class Sdk {
170
172
  }
171
173
  }
172
174
 
173
- module.exports = uri => {
174
- return new Sdk(uri).load();
175
+ module.exports = (uri, opts = {}) => {
176
+ return new Sdk(uri, opts).load();
175
177
  };