Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deprecating the dynamic api entrypoint #723

Merged
merged 8 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ updates:
ignore:
# The following are packages that we're ignoring updates from because they've moved to being
# ESM-only packages and we can't yet upgrade them.
- dependency-name: '@types/find-cache-dir'
versions:
- '>= 4'
- dependency-name: chalk
versions:
- '>= 5'
Expand All @@ -45,9 +42,6 @@ updates:
- dependency-name: figures
versions:
- '>= 4'
- dependency-name: find-cache-dir
versions:
- '>= 4'
- dependency-name: get-stream
versions:
- '>= 7'
Expand Down
6 changes: 2 additions & 4 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ category: 5d4c940cae4e610060475769

### Does this support YAML definitions?

Yes! YAML definitions will be automatically converted to JSON when they're fetched for either SDK code generation or dynamic `api` usage.
Yes! YAML definitions will be automatically converted to JSON when they're fetched.

### Does this support Swagger 2.0 definitions?

Expand All @@ -31,9 +31,7 @@ If you have ideas on how to handle this [we'd love to hear them](https://github.

### Will this work in browsers?

If you generate an SDK with the CLI installation process then yes! If you're having trouble getting autogenerated SDKs working in a browser, [please let us know](https://github.com/readmeio/api/issues)!

Unfortunately the dynamic version of `api` will **not** work in browsers as it requires access to the filesystem for handling and managing its cache state.
Yes! However because `api` supports `multipart/form-data` requests and uploading files to APIs you may need to polyfill the `fs` module in your toolchain. If you're having trouble getting autogenerated SDKs working in a browser [please let us know](https://github.com/readmeio/api/issues)!

### Will this validate my data before it reaches the API?

Expand Down
57 changes: 0 additions & 57 deletions docs/how-it-works.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ Behind the scenes, `api` will:
2. Dereference the definition so it's easier for us to handle.
3. Cache the definition so we don't need to re-fetch it.
4. Process the definition into chainable methods for HTTP verbs and operation IDs.
- If you're using the code generation offering this only done at compilation time. If you're using the dynamic `api` offering this is done whenever you call `api` by way of a JavaScript [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy).

## Fetching

At its core, `api` is a syntactical sugar wrapper for the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and the [HAR specification](http://www.softwareishard.com/blog/har-12-spec/). Parameters and payloads are compiled into a HAR according to the OpenAPI definition in question, using [@readme/oas-to-har](https://npm.im/@readme/oas-to-har) and then executed with [fetch-har](https://npm.im/fetch-har).

## Caching

Depending on the way you're using `api`, either dynamically or with code generation, there are two caching mechanisms in place:

- CLI code generation: `.api/`
- Dynamic usage: `node_modules/.cache/api`

### `.api/` directory

The `.api/` directory is where the CLI installation process stores all of its information: installation records, OpenAPI definitions, and code generated libraries. Its structure is as such:
Expand Down Expand Up @@ -66,54 +60,3 @@ The `api.json` file within `.api/` is where the CLI keeps track of everything th
```

In the future, commands will be added to the CLI that will take advantage of this information, but for now it's only used to determine if you've already installed an API to the same `identifier` (say, to prevent you from installing `@developers/v2.0#nysezql0wwo236` twice).

### `node_modules/.cache/api`

Because the dynamic version of `api` cannot have access to the `identifier` system that the CLI installation process does, the dynamic version of `api` has a slightly different caching mechanism and stores its data within `node_modules/.cache/api`:

```
.node_modules/
└── .cache/
└── api/
├── cache.json // Similar to `api.json` this is a record of
| // everything installed.
└── specs/
└── cbb821db3609f8983ce1a372dadd122c.json
```

> ⚠️
>
> Note that because the dynamic version of `api` requires a filesystem and the Node `crypto` module, `api` cannot be used in the browser. If you need to use it in a browser we recommend you use the code generation avenue instead.
#### `cache.json`

The `cache.json` file in `node_modules/.cache/api` is where the dynamic version of API stores and pulls all of its data from. OpenAPI definitions are indexed within this file by their original acessor (`require('api')('http://example.com/some-accessor')`).

The `hash` within this is an `md5` of the full OpenAPI definition that we retrieved.

If for some reason this file gets lost, or the accessor you're supplying to `api` changes for whatever reason `api` will re-retrieve the OpenAPI definition at run-time.

```json
{
"d6b93e95fa1a7efdce6d1406dc599923": {
"hash": "cbb821db3609f8983ce1a372dadd122c",
"original": "https://dash.readme.com/api/v1/api-registry/nysezql0wwo236",
"title": "API Endpoints",
"version": "2.0.0"
}
}
```

#### Custom cache directory

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 (e.g. if you use `npm prune`), you can customize this by supplying an additional argument to the `api` instantiator:

```js
const sdk = require('api')('https://raw.github.com/readmeio/oas-examples/main/3.0/json/petstore.json', {
cacheDir: './path/to/my/custom/cache/dir',
});

sdk.listPets().then(({ data })) => {
console.log(`My pets name is ${data[0].name}!`);
});
```
6 changes: 0 additions & 6 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,3 @@ The `api` installer will guide you through several prompts about what kind of pr
- URLs
- You can also access and supply the URL for your ReadMe API Registry entry by using https://dash.readme.com/api/v1/api-registry/{uuid}, where `{uuid}` is the last part of your registry entry string. So in `@developers/v2.0#nysezql0wwo236` that would be `nysezql0wwo236`.
- Local file paths

To use the dynamic version of `api`, install the package to your project dependencies:

```shell
$ npm install api --save
```
6 changes: 0 additions & 6 deletions docs/making-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ category: 62cc6ce22b8b6601da6cb12d

If the API you're using doesn't have any documented [operation IDs](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#user-content-operationid), `api` will generate some for you to use.

If you're using code generation, these will be immediately available to use in your generated library. However, due to the nature of the [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) architecture in the dynamic version of the library, this isn't the case for dynamically generated libraries. For these cases, you can check out the documentation for the API you're using.

> ⚠️
>
> We recommend using code generation as it'll give you the additional benefit of TypeScript type assistance and autocompletion (even if you aren't using TypeScript in your codebase).
With an instance of your SDK, you make an HTTP request against an operation on the API like so:

```js
Expand Down
2 changes: 1 addition & 1 deletion docs/upgrading-from-v4.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Upgrading from v4
title: Upgrading from v4 to v5
category: 5d4c940cae4e610060475769
---

Expand Down
35 changes: 0 additions & 35 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: Usage
category: 5d4c940cae4e610060475769
---

`api` can be used in two ways: a locally-installed code-generated library or dynamically.

## Code generation

The code generation installation process that `api` offers comes in the form of an `api` CLI that will:
Expand All @@ -31,36 +29,3 @@ petstore.listPets().then(({ data, status, headers, res }) => {
And if you use an IDE with TypeScript support (like [Visual Studio Code](https://code.visualstudio.com/)), you get the benefit of having autogenerated TypeScript types to help you out—regardless if you're actually using TypeScript!

![TypeScript types in action](https://raw.github.com/readmeio/api/main/docs/images/ts-types.png)

## Dynamically

If you don't wish to use code generation, you can load `api` and supply it an OpenAPI definition directly:

```js
const petstore = require('api')('https://raw.github.com/readmeio/oas-examples/main/3.0/json/petstore.json');

petstore.listPets().then(({ data, status, headers, res }) => {
console.log(`My pets name is ${data[0].name}!`);
});
```

Alternatively, you can use the ESM syntax:

```js
import api from 'api';
const petstore = api('@petstore/v1.0#tl1e4kl1cl8eg8');

petstore.listPets().then(({ data, status, headers, res }) => {
console.log(`My pets name is ${data[0].name}!`);
});
```

The OpenAPI definition is automatically downloaded, cached, and transformed into a chainable [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) Promise that you can use to make API requests.

> 📘
>
> By using the dynamic, non-code-generated version of `api`, you will not have access to any TypeScript types to assist you in using an API.
> ⚠️
>
> Using the dynamic version version of `api` will also gate you off you from using the library in a browser as the dynamic version of `api` requires access to a filesystem for the library to work.
39 changes: 7 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ petstore.listPets().then(({ data }) => {
});
```

Or you can use it dynamically (though you won't have fancy TypeScript types to help you out!):

```js
const petstore = require('api')(
'https://raw.github.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.json',
);

petstore.listPets().then(({ data }) => {
console.log(`My pets name is ${data[0].name}!`);
});
```

The ESM syntax is supported as well:

```js
Expand Down
13 changes: 0 additions & 13 deletions packages/api/example.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"name": "api",
"version": "6.1.1",
"description": "Magical SDK generation from an OpenAPI definition 🪄",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Comment on lines -5 to -6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:rip-cowboy:

"bin": {
"api": "./bin/api"
},
Expand Down Expand Up @@ -47,7 +45,6 @@
"execa": "^5.1.1",
"fetch-har": "^10.0.0",
"figures": "^3.2.0",
"find-cache-dir": "^3.3.1",
"get-stream": "^6.0.1",
"js-yaml": "^4.1.0",
"json-schema-to-ts": "^2.9.2",
Expand All @@ -70,7 +67,6 @@
"devDependencies": {
"@readme/oas-examples": "^5.12.0",
"@types/caseless": "^0.12.3",
"@types/find-cache-dir": "^3.2.1",
"@types/js-yaml": "^4.0.5",
"@types/lodash.camelcase": "^4.3.7",
"@types/lodash.deburr": "^4.1.7",
Expand Down
Loading