openapi-ff 0.1.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/LICENSE +21 -0
- package/README.md +119 -0
- package/dist/openapi-ff.cjs +1 -0
- package/dist/openapi-ff.d.cts +42 -0
- package/dist/openapi-ff.d.ts +42 -0
- package/dist/openapi-ff.js +50 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Oleg Borodatov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# openapi-ff
|
|
2
|
+
|
|
3
|
+
openapi-ff is a type-safe tiny wrapper around [effector](https://effector.dev/) and [farfetched](https://ff.effector.dev/) to work with OpenAPI schema.
|
|
4
|
+
|
|
5
|
+
It works by using [openapi-fetch](../openapi-fetch) and [openapi-typescript](../openapi-typescript).
|
|
6
|
+
|
|
7
|
+
## Setup
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm i openapi-ff @farfetched/core effector openapi-fetch
|
|
11
|
+
pnpm i -D openapi-typescript typescript
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Next, generate TypeScript types from your OpenAPI schema using openapi-typescript:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npx openapi-typescript ./path/to/api/v1.yaml -o ./src/shared/api/schema.d.ts
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import createFetchClient from "openapi-fetch";
|
|
24
|
+
import { createClient } from "openapi-ff";
|
|
25
|
+
import { createQuery } from "@farfetched/core";
|
|
26
|
+
import type { paths } from "./schema"; // generated by openapi-typescript
|
|
27
|
+
|
|
28
|
+
export const client = createFetchClient<paths>({
|
|
29
|
+
baseUrl: "https://myapi.dev/v1/",
|
|
30
|
+
});
|
|
31
|
+
export const { createApiEffect } = createClient(client);
|
|
32
|
+
|
|
33
|
+
const blogpostQuery = createQuery({
|
|
34
|
+
effect: createApiEffect("get", "/blogposts/{post_id}"),
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```tsx
|
|
39
|
+
import { useUnit } from "effector-react";
|
|
40
|
+
|
|
41
|
+
function Post() {
|
|
42
|
+
const { data: post, pending } = useUnit(blogpostQuery);
|
|
43
|
+
|
|
44
|
+
if (pending) {
|
|
45
|
+
return <Loader />;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<section>
|
|
50
|
+
<p>{post.title}</p>
|
|
51
|
+
</section>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Advanced Usage:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { chainRoute } from "atomic-router";
|
|
60
|
+
import { startChain } from "@farfetched/atomic-router";
|
|
61
|
+
|
|
62
|
+
const getBlogpostFx = createApiEffect("get", "/blogposts/{post_id}", {
|
|
63
|
+
mapParams: (args: { postId: string }) => ({ params: { path: { post_id: args.postId } } }),
|
|
64
|
+
});
|
|
65
|
+
const blogpostQuery = createQuery({
|
|
66
|
+
effect: getBlogpostFx,
|
|
67
|
+
mapData: ({ result, params }) => ({ ...result, ...params }),
|
|
68
|
+
initialData: { body: "-", title: "-", postId: "0" },
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
export const blogpostRoute = chainRoute({
|
|
72
|
+
route: routes.blogpost.item,
|
|
73
|
+
...startChain(blogpostQuery),
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const apiError = sample({
|
|
77
|
+
clock: softwareQuery.finished.failure,
|
|
78
|
+
filter: isApiError,
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Runtime Validation
|
|
83
|
+
|
|
84
|
+
`openapi-ff` does not handle runtime validation, as `openapi-typescript` [does not support it](https://github.com/openapi-ts/openapi-typescript/issues/1420#issuecomment-1792909086).
|
|
85
|
+
|
|
86
|
+
> openapi-typescript by its design generates runtime-free static types, and only static types.
|
|
87
|
+
|
|
88
|
+
To enable runtime validation, you can use third-party solutions, such as **[orval](https://orval.dev/)**:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
pnpm install zod @farfetched/zod
|
|
92
|
+
npx orval --input path/to/api.yaml --output src/zod.ts --client zod --mode single
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Usage:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { zodContract } from "@farfetched/zod";
|
|
99
|
+
import { getBlogpostsResponseItem } from "./zod";
|
|
100
|
+
|
|
101
|
+
const blogpostQuery = createQuery({
|
|
102
|
+
effect: createApiEffect("get", "/blogposts/{post_id}"),
|
|
103
|
+
contract: zodContract(getBlogpostsResponseItem),
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## TODO
|
|
108
|
+
|
|
109
|
+
Add `createApiQuery`:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
createApiQuery({
|
|
113
|
+
method: "get",
|
|
114
|
+
path: "/blogposts/{post_id}",
|
|
115
|
+
mapParams: (args: { postId: string }) => ({ params: { path: { post_id: args.postId } } }),
|
|
116
|
+
mapData: ({ result, params }) => ({ ...result, ...params }),
|
|
117
|
+
initialData: { body: "-", title: "-", postId: "0" },
|
|
118
|
+
});
|
|
119
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("effector"),o=require("@farfetched/core"),f="API";function w(r){return{...r,errorType:f,explanation:"Request was finished with unsuccessful HTTP code"}}function E(r){var s;return((s=r.error)==null?void 0:s.errorType)===f}function d(r){return{createApiEffect:(s,i,e)=>{const l=s.toUpperCase(),T=r[l];return p.createEffect(async u=>{var c;const{data:h,error:a,response:t}=await T(i,((c=e==null?void 0:e.mapParams)==null?void 0:c.call(e,u))??u).catch(n=>{throw o.networkError({reason:(n==null?void 0:n.message)??null,cause:n})});if(a!=null&&a!=="")throw w({status:t.status,statusText:t.statusText,response:a});if(!t.ok)throw o.httpError({status:t.status,statusText:t.statusText,response:null});return h})}}}exports.createClient=d;exports.isApiError=E;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Client } from 'openapi-fetch';
|
|
2
|
+
import { Effect } from 'effector';
|
|
3
|
+
import { FarfetchedError } from '@farfetched/core';
|
|
4
|
+
import { FetchResponse } from 'openapi-fetch';
|
|
5
|
+
import { HttpError } from '@farfetched/core';
|
|
6
|
+
import { HttpMethod } from 'openapi-typescript-helpers';
|
|
7
|
+
import { MaybeOptionalInit } from 'openapi-fetch';
|
|
8
|
+
import { MediaType } from 'openapi-typescript-helpers';
|
|
9
|
+
import { NetworkError } from '@farfetched/core';
|
|
10
|
+
import { PathsWithMethod } from 'openapi-typescript-helpers';
|
|
11
|
+
|
|
12
|
+
declare const API = "API";
|
|
13
|
+
|
|
14
|
+
export declare interface ApiError<Status extends number = number, ApiResponse = unknown> extends FarfetchedError<typeof API> {
|
|
15
|
+
status: Status;
|
|
16
|
+
statusText: string;
|
|
17
|
+
response: ApiResponse;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare type CreateApiEffect<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <Method extends HttpMethod, Path extends PathsWithMethod<Paths, Method>, Init extends MaybeOptionalInit<Paths[Path], Method>, Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, Options extends CreateApiEffectOptions<Init> = {}>(method: Method, path: Path, options?: Options) => Effect<Init extends undefined ? void : InitOrPrependInit<Init, Parameters<NonNullable<Options["mapParams"]>>[0], Options>, Response["data"], ApiError<number, Response["error"]> | HttpError | NetworkError>;
|
|
21
|
+
|
|
22
|
+
declare type CreateApiEffectOptions<Init> = {
|
|
23
|
+
mapParams?: (init: any) => Init;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export declare function createClient<Paths extends {}, Media extends MediaType = MediaType>(client: Client<Paths, Media>): OpenapiEffectorClient<Paths, Media>;
|
|
27
|
+
|
|
28
|
+
declare type InitOrPrependInit<Init, PrependInit, Options extends CreateApiEffectOptions<Init>> = Options extends {
|
|
29
|
+
mapParams: (init: any) => any;
|
|
30
|
+
} ? PrependInit : Init;
|
|
31
|
+
|
|
32
|
+
export declare function isApiError(args: WithError): args is WithError<ApiError>;
|
|
33
|
+
|
|
34
|
+
declare interface OpenapiEffectorClient<Paths extends {}, Media extends MediaType = MediaType> {
|
|
35
|
+
createApiEffect: CreateApiEffect<Paths, Media>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
declare type WithError<T = any, P = Record<string, unknown>> = P & {
|
|
39
|
+
error: T;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { }
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Client } from 'openapi-fetch';
|
|
2
|
+
import { Effect } from 'effector';
|
|
3
|
+
import { FarfetchedError } from '@farfetched/core';
|
|
4
|
+
import { FetchResponse } from 'openapi-fetch';
|
|
5
|
+
import { HttpError } from '@farfetched/core';
|
|
6
|
+
import { HttpMethod } from 'openapi-typescript-helpers';
|
|
7
|
+
import { MaybeOptionalInit } from 'openapi-fetch';
|
|
8
|
+
import { MediaType } from 'openapi-typescript-helpers';
|
|
9
|
+
import { NetworkError } from '@farfetched/core';
|
|
10
|
+
import { PathsWithMethod } from 'openapi-typescript-helpers';
|
|
11
|
+
|
|
12
|
+
declare const API = "API";
|
|
13
|
+
|
|
14
|
+
export declare interface ApiError<Status extends number = number, ApiResponse = unknown> extends FarfetchedError<typeof API> {
|
|
15
|
+
status: Status;
|
|
16
|
+
statusText: string;
|
|
17
|
+
response: ApiResponse;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare type CreateApiEffect<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <Method extends HttpMethod, Path extends PathsWithMethod<Paths, Method>, Init extends MaybeOptionalInit<Paths[Path], Method>, Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, Options extends CreateApiEffectOptions<Init> = {}>(method: Method, path: Path, options?: Options) => Effect<Init extends undefined ? void : InitOrPrependInit<Init, Parameters<NonNullable<Options["mapParams"]>>[0], Options>, Response["data"], ApiError<number, Response["error"]> | HttpError | NetworkError>;
|
|
21
|
+
|
|
22
|
+
declare type CreateApiEffectOptions<Init> = {
|
|
23
|
+
mapParams?: (init: any) => Init;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export declare function createClient<Paths extends {}, Media extends MediaType = MediaType>(client: Client<Paths, Media>): OpenapiEffectorClient<Paths, Media>;
|
|
27
|
+
|
|
28
|
+
declare type InitOrPrependInit<Init, PrependInit, Options extends CreateApiEffectOptions<Init>> = Options extends {
|
|
29
|
+
mapParams: (init: any) => any;
|
|
30
|
+
} ? PrependInit : Init;
|
|
31
|
+
|
|
32
|
+
export declare function isApiError(args: WithError): args is WithError<ApiError>;
|
|
33
|
+
|
|
34
|
+
declare interface OpenapiEffectorClient<Paths extends {}, Media extends MediaType = MediaType> {
|
|
35
|
+
createApiEffect: CreateApiEffect<Paths, Media>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
declare type WithError<T = any, P = Record<string, unknown>> = P & {
|
|
39
|
+
error: T;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { }
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createEffect as p } from "effector";
|
|
2
|
+
import { networkError as T, httpError as i } from "@farfetched/core";
|
|
3
|
+
const f = "API";
|
|
4
|
+
function w(r) {
|
|
5
|
+
return {
|
|
6
|
+
...r,
|
|
7
|
+
errorType: f,
|
|
8
|
+
explanation: "Request was finished with unsuccessful HTTP code"
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function A(r) {
|
|
12
|
+
var s;
|
|
13
|
+
return ((s = r.error) == null ? void 0 : s.errorType) === f;
|
|
14
|
+
}
|
|
15
|
+
function P(r) {
|
|
16
|
+
return {
|
|
17
|
+
createApiEffect: (s, c, t) => {
|
|
18
|
+
const l = s.toUpperCase(), h = r[l];
|
|
19
|
+
return p(async (u) => {
|
|
20
|
+
var o;
|
|
21
|
+
const { data: m, error: n, response: e } = await h(
|
|
22
|
+
c,
|
|
23
|
+
((o = t == null ? void 0 : t.mapParams) == null ? void 0 : o.call(t, u)) ?? u
|
|
24
|
+
).catch((a) => {
|
|
25
|
+
throw T({
|
|
26
|
+
reason: (a == null ? void 0 : a.message) ?? null,
|
|
27
|
+
cause: a
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
if (n != null && n !== "")
|
|
31
|
+
throw w({
|
|
32
|
+
status: e.status,
|
|
33
|
+
statusText: e.statusText,
|
|
34
|
+
response: n
|
|
35
|
+
});
|
|
36
|
+
if (!e.ok)
|
|
37
|
+
throw i({
|
|
38
|
+
status: e.status,
|
|
39
|
+
statusText: e.statusText,
|
|
40
|
+
response: null
|
|
41
|
+
});
|
|
42
|
+
return m;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export {
|
|
48
|
+
P as createClient,
|
|
49
|
+
A as isApiError
|
|
50
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "openapi-ff",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "vitest run --typecheck",
|
|
7
|
+
"build": "vite build",
|
|
8
|
+
"format": "biome format --write ./src",
|
|
9
|
+
"lint": "biome check --apply ./src"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"main": "./dist/openapi-ff.cjs",
|
|
16
|
+
"module": "./dist/openapi-ff.js",
|
|
17
|
+
"types": "./dist/openapi-ff.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": {
|
|
21
|
+
"types": "./dist/openapi-ff.d.ts",
|
|
22
|
+
"default": "./dist/openapi-ff.js"
|
|
23
|
+
},
|
|
24
|
+
"require": {
|
|
25
|
+
"types": "./dist/openapi-ff.d.cts",
|
|
26
|
+
"default": "./dist/openapi-ff.cjs"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@biomejs/biome": "^1.9.4",
|
|
32
|
+
"@types/node": "^20.12.7",
|
|
33
|
+
"typescript": "5.1.6",
|
|
34
|
+
"undici": "^6.21.0",
|
|
35
|
+
"vite": "5",
|
|
36
|
+
"vite-plugin-dts": "^3.8.3",
|
|
37
|
+
"vite-tsconfig-paths": "4.2.1",
|
|
38
|
+
"vitest": "2.0.4"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@farfetched/core": "^0.12.8",
|
|
42
|
+
"effector": "^23.2.3"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"openapi-fetch": "^0.13.0",
|
|
46
|
+
"openapi-typescript-helpers": "^0.0.15"
|
|
47
|
+
}
|
|
48
|
+
}
|