Skip to content
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Automatic SDK generation from an OpenAPI definition.
* [Authentication](#authentication)
* [Parameters and Payloads](#parameters-and-payloads)
* [HTTP requests](#http-requests)
* [Server configurations](#server-configurations)
* [How does it work?](#how-does-it-work)
* [Interested in contributing?](#interested-in-contributing)
* [FAQ](#faq)
Expand Down Expand Up @@ -97,6 +98,24 @@ sdk.get('/pets/{petId}', { petId: 1234 }).then(...)

The SDK supports GET, PUT, POST, DELETE, OPTIONS, HEAD, and TRACE requests.

### Server configurations
If the API you're using offers alternate server URLs and server variables in its [`servers`](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#serverObject) definition you can supply this to the SDK with `.server()`:

```js
sdk.server('https://{region}.api.example.com/{basePath}', {
name: 'eu',
basePath: 'v14',
});

sdk.get('/pets').then(...)
```

When your request is executed it will be made to `https://eu.api.example.com/v14/pets`. Alternatively if you don't want to deal with URL templates you can opt to pass the full URL in instead:

```js
sdk.server('https://eu.api.example.com/v14');
```

## How does it work?
Behind the scenes, `api` will:

Expand Down Expand Up @@ -135,3 +154,10 @@ Not yet! The URL that you give the module must be publicy accessible. If it isn'
```js
const sdk = require('api')('/path/to/downloaded.json');
```

#### How do I access the Response object (for status and headers)?
By default we parse the response based on the `content-type` header for you. You can disable this by doing the following:

```js
sdk.config({ parseResponse: false });
```
17 changes: 9 additions & 8 deletions package-lock.json

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

34 changes: 19 additions & 15 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Automatic SDK generation from an OpenAPI definition.
* [Authentication](#authentication)
* [Parameters and Payloads](#parameters-and-payloads)
* [HTTP requests](#http-requests)
* [Server configurations](#server-configurations)
* [How does it work?](#how-does-it-work)
* [Interested in contributing?](#interested-in-contributing)
* [FAQ](#faq)
Expand All @@ -24,7 +25,7 @@ Using `api` is as simple as supplying it an OpenAPI and using the SDK as you wou
```js
const sdk = require('api')('https://raw.githubusercontent.com/readmeio/oas/master/packages/examples/3.0/json/petstore.json');

sdk.listPets().then(res => {
sdk.listPets().then(res => res.json()).then(res => {
console.log(`My pets name is ${res[0].name}!`);
});
```
Expand Down Expand Up @@ -88,30 +89,33 @@ You can also give it a stream and it'll handle all of the hard work for you.
sdk.uploadFile({ file: fs.createReadStream('/path/to/a/file.txt') }).then(...)
```

### Responses
Since we know the `Content-Type` of the returned response, we automatically parse it for you before returning it. So no more superfluous `.then(res => res.json())` calls. If your API returned with JSON, we'll give you the parsed JSON.
### HTTP requests
If the API you're using doesn't have any documented operation IDs, you can make requests with HTTP verbs instead:

```js
sdk.get('/pets/{petId}', { petId: 1234 }).then(...)
```

The SDK supports GET, PUT, POST, DELETE, OPTIONS, HEAD, and TRACE requests.

If you need access to the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object you can disable our automatic parsing using `.config()` like so:
### Server configurations
If the API you're using offers alternate server URLs and server variables in its [`servers`](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#serverObject) definition you can supply this to the SDK with `.server()`:

```js
sdk.config({ parseResponse: false });
sdk.server('https://{region}.api.example.com/{basePath}', {
name: 'eu',
basePath: 'v14',
});

sdk.createPets({ name: 'Buster' })
.then(res => {
// `res` will be a Response object
// so you can access status and headers
})
sdk.get('/pets').then(...)
```

### HTTP requests
If the API you're using doesn't have any documented operation IDs, you can make requests with HTTP verbs instead:
When your request is executed it will be made to `https://eu.api.example.com/v14/pets`. Alternatively if you don't want to deal with URL templates you can opt to pass the full URL in instead:

```js
sdk.get('/pets/{petId}', { petId: 1234 }).then(...)
sdk.server('https://eu.api.example.com/v14');
```

The SDK supports GET, PUT, POST, DELETE, OPTIONS, HEAD, and TRACE requests.

## How does it work?
Behind the scenes, `api` will:

Expand Down
20 changes: 0 additions & 20 deletions packages/api/__tests__/__fixtures__/createOas.js

This file was deleted.

58 changes: 58 additions & 0 deletions packages/api/__tests__/__fixtures__/payloads.oas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"openapi": "3.0.0",
"info": {
"title": "OAS test cases",
"version": "1.0.0"
},
"servers": [
{
"url": "https://api.example.com"
}
],
"paths": {
"/arraySchema": {
"put": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
}
},
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/primitiveBody": {
"put": {
"requestBody": {
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
},
"responses": {
"200": {
"description": "OK"
}
}
}
}
}
}
Loading