schemock 0.0.4-alpha.6 → 0.0.4-alpha.8
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/README.md +50 -0
- package/dist/cli/index.js +325 -200
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/index.mjs +325 -200
- package/dist/cli/index.mjs.map +1 -1
- package/dist/cli.js +333 -200
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1148,6 +1148,56 @@ const user = await api.user.get('123');
|
|
|
1148
1148
|
await api.post.create({ title: 'Hello', authorId: '123' });
|
|
1149
1149
|
```
|
|
1150
1150
|
|
|
1151
|
+
### Using with React Hooks (SchemockProvider)
|
|
1152
|
+
|
|
1153
|
+
To use the configured client with generated React hooks, wrap your app with `SchemockProvider`:
|
|
1154
|
+
|
|
1155
|
+
```tsx
|
|
1156
|
+
import { SchemockProvider, createClient, useUsers, useCreateUser } from './generated';
|
|
1157
|
+
|
|
1158
|
+
// 1. Create configured client
|
|
1159
|
+
const api = createClient({
|
|
1160
|
+
onRequest: (ctx) => {
|
|
1161
|
+
const token = localStorage.getItem('authToken');
|
|
1162
|
+
if (token) {
|
|
1163
|
+
ctx.headers.Authorization = `Bearer ${token}`;
|
|
1164
|
+
}
|
|
1165
|
+
return ctx;
|
|
1166
|
+
},
|
|
1167
|
+
onError: (error) => {
|
|
1168
|
+
if (error.status === 401) window.location.href = '/login';
|
|
1169
|
+
}
|
|
1170
|
+
});
|
|
1171
|
+
|
|
1172
|
+
// 2. Wrap your app
|
|
1173
|
+
function App() {
|
|
1174
|
+
return (
|
|
1175
|
+
<SchemockProvider client={api}>
|
|
1176
|
+
<UserList />
|
|
1177
|
+
</SchemockProvider>
|
|
1178
|
+
);
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
// 3. Hooks automatically use the configured client
|
|
1182
|
+
function UserList() {
|
|
1183
|
+
const { data, isLoading } = useUsers();
|
|
1184
|
+
const createUser = useCreateUser();
|
|
1185
|
+
|
|
1186
|
+
if (isLoading) return <div>Loading...</div>;
|
|
1187
|
+
|
|
1188
|
+
return (
|
|
1189
|
+
<div>
|
|
1190
|
+
{data?.data.map(user => <div key={user.id}>{user.name}</div>)}
|
|
1191
|
+
<button onClick={() => createUser.mutate({ name: 'New User' })}>
|
|
1192
|
+
Add User
|
|
1193
|
+
</button>
|
|
1194
|
+
</div>
|
|
1195
|
+
);
|
|
1196
|
+
}
|
|
1197
|
+
```
|
|
1198
|
+
|
|
1199
|
+
Without `SchemockProvider`, hooks use the default unconfigured client (no auth).
|
|
1200
|
+
|
|
1151
1201
|
### Creating Mock JWT Tokens
|
|
1152
1202
|
|
|
1153
1203
|
For testing different user contexts:
|