Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0c6075f

Browse files
committedDec 15, 2021
Update readme
1 parent f61f041 commit 0c6075f

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed
 

‎readme.md

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ A library for quickly created SDKs for your Node.js Typescript backends. No buil
66

77
### Server
88

9-
Define a server file like so:
9+
#### 1. Define your backend as an arbitrarily deep object of async functions
1010

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.
1312

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
1514

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
1717

18-
//In this example we namespace by POST and GET properties on the API but this is simply a convention
19-
const myApi = {
18+
export const myApi = {
2019
accounts: {
2120
GET: {
2221
async getById(p: { id: string }) {
@@ -45,26 +44,31 @@ const myApi = {
4544
};
4645

4746
export type MyApiType = typeof myApi
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
4855

49-
// 2. Hook up your API to an actual server that listens for requests, such as fastify or express
56+
import { myApi } from "./api.ts";
5057
import { collectEndpoints } from "create-typed-sdk/server";
5158
import fastify from "fastify";
5259
import cors from "fastify-cors";
5360
const app = fastify();
5461

55-
app.register(cors, { origin: "*"});
62+
app.register(cors, { origin: "*" });
5663

5764
collectEndpoints(myApi).forEach(({ fn, path }) => {
5865
const method = path[path.length - 2].toLowerCase();
5966

60-
app[method](
61-
"/" + path.join("/"),
62-
async (req, resp) => {
63-
const data = method === 'get' ? req.query : req.body
64-
const val = await fn(data);
65-
resp.send(val);
66-
}
67-
);
67+
app[method]("/" + path.join("/"), async (req, resp) => {
68+
const data = method === "get" ? req.query : req.body;
69+
const val = await fn(data);
70+
resp.send(val);
71+
});
6872
});
6973

7074
(async () => {
@@ -77,14 +81,24 @@ collectEndpoints(myApi).forEach(({ fn, path }) => {
7781
})();
7882
```
7983

80-
### Frontend
84+
#### 3. Automagically create an SDK for consumers of your backend
8185

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.
8592
import axios from 'axios';
86-
function doFetch({ path, argument }) {
93+
import type { 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 = new QueryClient();
8798

99+
const MyServerSDK = createTypedSDK<ApiType>({
100+
queryClient, //Add an optional react-query dependency for easy data fetching in React land
101+
doFetch({ path, argument }) {
88102
const method = path[1]
89103
const data = method === 'GET' ? {params: argument} : {body: argument}
90104
return axios(`http://my-api-server.com/${path.join("/")}`, {
@@ -98,27 +112,21 @@ function doFetch({ path, argument }) {
98112

99113
return resp.json();
100114
});
101-
}
115+
},
116+
});
117+
```
102118

103-
// 4. Import your Types and create the SDK
104-
import type { 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
107120

108-
const queryClient = new QueryClient();
121+
```TSX
122+
// App.tsx
109123

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'
114125

115-
// 5. Use it!
116126
function App(){
117-
118127
// 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
119128
const resp = MyServerSDK.useEndpoint().accounts.GET.getById({id: "some-user})
120129

121130
return <div>Hello, my name is {resp.data.name}</div>
122-
123131
}
124132
```

0 commit comments

Comments
 (0)
Please sign in to comment.