Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openapi-fetch

Package Overview
Dependencies
Maintainers
1
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-fetch - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

6

CHANGELOG.md
# openapi-fetch
## 0.1.2
### Patch Changes
- e730cd8: Fix post() and options() types
## 0.1.1

@@ -4,0 +10,0 @@

4

dist/index.d.ts

@@ -80,7 +80,7 @@ /** options for each client instance */

/** Call a POST endpoint */
post<P_2 extends PathsWith<Paths, "post">>(url: P_2, init: FetchOptions<FilterKeys<Paths[P_2], "post">>): Promise<FetchResponse<"put" extends infer T_2 ? T_2 extends "put" ? T_2 extends keyof Paths[P_2] ? Paths[P_2][T_2] : unknown : never : never>>;
post<P_2 extends PathsWith<Paths, "post">>(url: P_2, init: FetchOptions<FilterKeys<Paths[P_2], "post">>): Promise<FetchResponse<"post" extends infer T_2 ? T_2 extends "post" ? T_2 extends keyof Paths[P_2] ? Paths[P_2][T_2] : unknown : never : never>>;
/** Call a DELETE endpoint */
del<P_3 extends PathsWith<Paths, "delete">>(url: P_3, init: FetchOptions<FilterKeys<Paths[P_3], "delete">>): Promise<FetchResponse<"delete" extends infer T_3 ? T_3 extends "delete" ? T_3 extends keyof Paths[P_3] ? Paths[P_3][T_3] : unknown : never : never>>;
/** Call a OPTIONS endpoint */
options<P_4 extends PathsWith<Paths, "options">>(url: P_4, init: FetchOptions<FilterKeys<Paths[P_4], "options">>): Promise<FetchResponse<"delete" extends infer T_4 ? T_4 extends "delete" ? T_4 extends keyof Paths[P_4] ? Paths[P_4][T_4] : unknown : never : never>>;
options<P_4 extends PathsWith<Paths, "options">>(url: P_4, init: FetchOptions<FilterKeys<Paths[P_4], "options">>): Promise<FetchResponse<"options" extends infer T_4 ? T_4 extends "options" ? T_4 extends keyof Paths[P_4] ? Paths[P_4][T_4] : unknown : never : never>>;
/** Call a HEAD endpoint */

@@ -87,0 +87,0 @@ head<P_5 extends PathsWith<Paths, "head">>(url: P_5, init: FetchOptions<FilterKeys<Paths[P_5], "head">>): Promise<FetchResponse<"head" extends infer T_5 ? T_5 extends "head" ? T_5 extends keyof Paths[P_5] ? Paths[P_5][T_5] : unknown : never : never>>;

@@ -80,7 +80,7 @@ /** options for each client instance */

/** Call a POST endpoint */
post<P_2 extends PathsWith<Paths, "post">>(url: P_2, init: FetchOptions<FilterKeys<Paths[P_2], "post">>): Promise<FetchResponse<"put" extends infer T_2 ? T_2 extends "put" ? T_2 extends keyof Paths[P_2] ? Paths[P_2][T_2] : unknown : never : never>>;
post<P_2 extends PathsWith<Paths, "post">>(url: P_2, init: FetchOptions<FilterKeys<Paths[P_2], "post">>): Promise<FetchResponse<"post" extends infer T_2 ? T_2 extends "post" ? T_2 extends keyof Paths[P_2] ? Paths[P_2][T_2] : unknown : never : never>>;
/** Call a DELETE endpoint */
del<P_3 extends PathsWith<Paths, "delete">>(url: P_3, init: FetchOptions<FilterKeys<Paths[P_3], "delete">>): Promise<FetchResponse<"delete" extends infer T_3 ? T_3 extends "delete" ? T_3 extends keyof Paths[P_3] ? Paths[P_3][T_3] : unknown : never : never>>;
/** Call a OPTIONS endpoint */
options<P_4 extends PathsWith<Paths, "options">>(url: P_4, init: FetchOptions<FilterKeys<Paths[P_4], "options">>): Promise<FetchResponse<"delete" extends infer T_4 ? T_4 extends "delete" ? T_4 extends keyof Paths[P_4] ? Paths[P_4][T_4] : unknown : never : never>>;
options<P_4 extends PathsWith<Paths, "options">>(url: P_4, init: FetchOptions<FilterKeys<Paths[P_4], "options">>): Promise<FetchResponse<"options" extends infer T_4 ? T_4 extends "options" ? T_4 extends keyof Paths[P_4] ? Paths[P_4][T_4] : unknown : never : never>>;
/** Call a HEAD endpoint */

@@ -87,0 +87,0 @@ head<P_5 extends PathsWith<Paths, "head">>(url: P_5, init: FetchOptions<FilterKeys<Paths[P_5], "head">>): Promise<FetchResponse<"head" extends infer T_5 ? T_5 extends "head" ? T_5 extends keyof Paths[P_5] ? Paths[P_5][T_5] : unknown : never : never>>;

{
"name": "openapi-fetch",
"version": "0.1.1",
"version": "0.1.2",
"author": {

@@ -5,0 +5,0 @@ "name": "Drew Powers",

@@ -11,3 +11,3 @@ # 🎾 openapi-fetch

\* _Note: codegen depends on the scope of your API: the larger it is, the larger your client weight. This is the actual weight of GitHub’s REST API client._
\* _Note: the larger your schema, the larger your codegen size. This is the actual weight of GitHub’s REST API._

@@ -20,6 +20,5 @@ The syntax is inspired by popular libraries like react-query or Apollo client, but without all the bells and whistles and in a 1 kb package.

const { get, post } = createClient<paths>();
const { get, post } = createClient<paths>({ baseUrl: 'https://myapi.dev/v1/' });
// Type-checked request
await post('/create-post', {

@@ -33,3 +32,2 @@ body: {

// Type-checked response
const { data, error } = await get('/post/my-blog-post');

@@ -44,8 +42,7 @@

- ✅ No malformed URLs
- ✅ Always using the correct method
- ✅ All parameters are fully type-checked and matched the schema
- ✅ For POST and PATCH, etc., all request bodies are fully type-checked as well
- ✅ No chance the wrong type was manually imported
- ✅ No chance typing was bypassed altogether
- ✅ No typos in URLs or params
- ✅ All parameters, request bodies, and responses are type-checked and 100% match your schema
- ✅ No manual typing of your API
- ✅ Eliminates `any` types that hide bugs
- ✅ Also eliminates `as` type overrides that can also hide bugs
- ✅ All of this in a **1 kB** client package 🎉

@@ -79,3 +76,3 @@

headers: {
Authorization: `Bearer ${import.meta.env.VITE_AUTH_TOKEN}`,
Authorization: `Bearer ${myAuthToken}`,
},

@@ -89,41 +86,5 @@ });

```yaml
# v1.yaml
paths:
/post/{post_id}:
get:
parameters:
- in: path
name: post_id
required: true
- in: query
name: version
responses:
200: #…
404: #…
/create-post:
post:
requestBody:
required: true
schema:
content:
application/json:
type: object
properties:
title:
type: string
body:
type: string
publish_date:
type: number
required:
- title
- body
- publish_date
responses:
200: #…
500: #…
```
![OpenAPI schema example](.github/images/example.png)
Here’s how you’d query either endpoint:
Here’s how you’d fetch both endpoints:

@@ -134,3 +95,3 @@ ```ts

const { get, post } = createClient<paths>();
const { get, post } = createClient<paths>({ baseUrl: 'https://myapi.dev/v1/' });

@@ -155,3 +116,9 @@ // GET /post/{post_id}

Note in the `get()` example, the URL was actually `/post/{post_id}`, _not_ `/post/my-post`. The URL matched the OpenAPI schema definition rather than the final URL. This library will replace the path param correctly for you, automatically.
- The URL **must match the actual schema** (`/post/{post_id}`). This library replaces all **path** params for you (so they can be typechecked).
- The `params` object will contain your `path` and `query` parameters, enforcing the correct types.
- The request `body` will only be required if the endpoint needs it.
- The endpoint will then respond with **data**, **error**, and **response**.
- **data** will contain your typechecked successful response if the request succeeded (`2xx`); otherwise it will be `undefined`
- **error** likewise contains your typechecked error response if the request failed (`4xx` / `5xx`); otherwise it will be `undefined`
- **response** has other information about the request such as `status`, `headers`, etc. It is not typechecked.

@@ -166,3 +133,3 @@ ### 🔀 Parameter Serialization

const { get, post } = createClient<paths>();
const { get, post } = createClient<paths>({ baseUrl: 'https://myapi.dev/v1/' });

@@ -174,3 +141,3 @@ const { data, error } = await get('/post/{post_id}', {

},
querySerializer: (q) => `v=${q.version}`,
querySerializer: (q) => `v=${q.version}`, // ✅ Still typechecked based on the URL!
});

@@ -199,2 +166,3 @@ ```

headers: currentToken ? { Authorization: `Bearer ${currentToken}` } : {},
baseUrl: 'https://myapi.dev/v1/',
})

@@ -225,6 +193,6 @@ );

const baseClient = createClient<paths>();
const baseClient = createClient<paths>({ baseUrl: 'https://myapi.dev/v1/' });
export default new Proxy(baseClient, {
get(_, key: keyof typeof baseClient) {
const newClient = createClient<paths>({ headers: authToken ? { Authorization: `Bearer ${authToken}` } : {} });
const newClient = createClient<paths>({ headers: authToken ? { Authorization: `Bearer ${authToken}` } : {}, baseUrl: 'https://myapi.dev/v1/' });
return newClient[key];

@@ -244,9 +212,12 @@ },

`createClient()` accepts the following options, which set the default settings for all subsequent fetch calls.
**createClient** accepts the following options, which set the default settings for all subsequent fetch calls.
| Name | Type | Description |
| :-------- | :------: | :-------------------------------------- |
| `baseUrl` | `string` | Prefix all fetch URLs with this option. |
```ts
createClient<paths>(options);
```
In addition, you may pass any other [fetch options](https://developer.mozilla.org/en-US/docs/Web/API/fetch) such as `headers`, `mode`, `credentials`, `redirect`, etc. ([docs](https://developer.mozilla.org/en-US/docs/Web/API/fetch)).
| Name | Type | Description |
| :-------------- | :------: | :--------------------------------------------------------------------------------------------------------------------------------------- |
| `baseUrl` | `string` | Prefix all fetch URLs with this option (e.g. `"https://myapi.dev/v1/"`). |
| (Fetch options) | | Any valid fetch option (`headers`, `mode`, `cache`, `signal` …) ([docs](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)) |

@@ -253,0 +224,0 @@ ## 🎯 Project Goals

@@ -102,3 +102,3 @@ // settings

async post<P extends PathsWith<Paths, 'post'>>(url: P, init: FetchOptions<FilterKeys<Paths[P], 'post'>>) {
return coreFetch<P, 'put'>(url, { ...init, method: 'POST' } as any);
return coreFetch<P, 'post'>(url, { ...init, method: 'POST' } as any);
},

@@ -111,3 +111,3 @@ /** Call a DELETE endpoint */

async options<P extends PathsWith<Paths, 'options'>>(url: P, init: FetchOptions<FilterKeys<Paths[P], 'options'>>) {
return coreFetch<P, 'delete'>(url, { ...init, method: 'OPTIONS' } as any);
return coreFetch<P, 'options'>(url, { ...init, method: 'OPTIONS' } as any);
},

@@ -114,0 +114,0 @@ /** Call a HEAD endpoint */

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc