You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+43-35Lines changed: 43 additions & 35 deletions
Original file line number
Diff line number
Diff line change
@@ -6,17 +6,16 @@ A library for quickly created SDKs for your Node.js Typescript backends. No buil
6
6
7
7
### Server
8
8
9
-
Define a server file like so:
9
+
#### 1. Define your backend as an arbitrarily deep object of async functions
10
10
11
-
```typescript
12
-
// server.ts
11
+
Note: This object can have ANY structure. The only requirements are (a) that the object leaf nodes are promise returning functions and (b) those functions only accept a SINGLE parameter.
13
12
14
-
// 1. Define your backend as an arbitrarily deep object of async functions
13
+
In this example we namespace by POST and GET properties on the API but this is simply a convention
15
14
16
-
//Note: This object can have ANY structure. The only requirements are (a) that the object leaf nodes are promise returning functions and (b) those functions only accept a SINGLE parameter.
15
+
```typescript
16
+
// api.ts
17
17
18
-
//In this example we namespace by POST and GET properties on the API but this is simply a convention
19
-
const myApi = {
18
+
exportconst myApi = {
20
19
accounts: {
21
20
GET: {
22
21
async getById(p: { id:string }) {
@@ -45,26 +44,31 @@ const myApi = {
45
44
};
46
45
47
46
exporttypeMyApiType=typeofmyApi
47
+
```
48
+
49
+
#### 2. Hook up the API to an actual server that listens for requests, such as fastify or express
50
+
51
+
Note: Make sure you adhere to the conventions of the deep API object you defined.
52
+
53
+
```typescript
54
+
// server.ts
48
55
49
-
// 2. Hook up your API to an actual server that listens for requests, such as fastify or express
#### 3. Automagically create an SDK for consumers of your backend
81
85
82
-
```TSX
83
-
// 3. Define your rules and transport for translating a path (like `accounts.GET.getById`) into an actual request
84
-
// Note that the assumptions of your backend must be adhered to here...
86
+
When creating the SDK, you must define the rules and transport that will allow a simple object path (like `accounts.GET.getById`) and an argument to be translated an actual request.
87
+
88
+
```typescript
89
+
// sdk.tsx
90
+
91
+
//Note: This file is typically going to be the 'main' file of your backend's package.json. This file is what external consumers of your API will be importing.
85
92
importaxiosfrom'axios';
86
-
function doFetch({ path, argument }) {
93
+
importtype { MyApiType } from'./api'//IMPORTANT NOTE: Only import the api TYPE, not the api itself so as not to expose server details to your client
94
+
import { createTypedSDK } from"create-typed-sdk";
95
+
import { QueryClient } from"react-query";
96
+
97
+
const queryClient =newQueryClient();
87
98
99
+
const MyServerSDK =createTypedSDK<ApiType>({
100
+
queryClient, //Add an optional react-query dependency for easy data fetching in React land
101
+
doFetch({ path, argument }) {
88
102
const method =path[1]
89
103
const data =method==='GET'? {params: argument} : {body: argument}
@@ -98,27 +112,21 @@ function doFetch({ path, argument }) {
98
112
99
113
returnresp.json();
100
114
});
101
-
}
115
+
},
116
+
});
117
+
```
102
118
103
-
// 4. Import your Types and create the SDK
104
-
importtype { MyApiType } from'./my-server'//IMPORTANT NOTE: Only import the api TYPE, not the api itself so as not to expose server details to your client
105
-
import { createTypedSDK } from"create-typed-sdk";
106
-
import { QueryClient } from"react-query";
119
+
#### 4. Consume the SDK in your frontend
107
120
108
-
const queryClient =newQueryClient();
121
+
```TSX
122
+
// App.tsx
109
123
110
-
const MyServerSDK =createTypedSDK<ApiType>({
111
-
queryClient, //Optional react-query dependency for easy data fetching in React land
112
-
doFetch,
113
-
});
124
+
import { MyServerSDK } from'./sdk'
114
125
115
-
// 5. Use it!
116
126
function App(){
117
-
118
127
// Note: It is recommended that your SDK be an uppercase variable like `MyServerSDK` (e.g a namespace) so that the hook usage below will be linted per the rules of hooks
0 commit comments