schemock 0.0.4-alpha.5 → 0.0.4-alpha.6
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 +126 -0
- package/dist/cli/index.js +276 -116
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/index.mjs +276 -116
- package/dist/cli/index.mjs.map +1 -1
- package/dist/cli.js +276 -116
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1089,6 +1089,118 @@ For custom endpoints:
|
|
|
1089
1089
|
└── endpoint-resolvers.ts # Mock resolvers
|
|
1090
1090
|
```
|
|
1091
1091
|
|
|
1092
|
+
### ⚠️ Warning: Do Not Modify Generated Files
|
|
1093
|
+
|
|
1094
|
+
**NEVER edit files in `src/generated/` directly.** These files are auto-generated and will be overwritten on next `npx schemock generate`.
|
|
1095
|
+
|
|
1096
|
+
To make changes:
|
|
1097
|
+
1. Edit your schema files in `src/schemas/`
|
|
1098
|
+
2. Run `npx schemock generate`
|
|
1099
|
+
3. Your changes will be reflected in the regenerated output
|
|
1100
|
+
|
|
1101
|
+
If you find a bug in generated code, please [report it](https://github.com/prajyot-tote/schemock/issues) rather than editing the output directly.
|
|
1102
|
+
|
|
1103
|
+
## Mock Client - Authentication & Error Handling
|
|
1104
|
+
|
|
1105
|
+
The generated mock client uses a production-ready **interceptor pattern** for centralized auth and error handling - just like axios interceptors or fetch wrappers in real applications.
|
|
1106
|
+
|
|
1107
|
+
### Setup (Configure Once)
|
|
1108
|
+
|
|
1109
|
+
```typescript
|
|
1110
|
+
import { createClient } from './generated/client';
|
|
1111
|
+
import { createMockJwt } from 'schemock/middleware';
|
|
1112
|
+
|
|
1113
|
+
// Configure at app startup (e.g., in _app.tsx or main.tsx)
|
|
1114
|
+
export const api = createClient({
|
|
1115
|
+
// Runs before every API call - add auth headers here
|
|
1116
|
+
onRequest: (ctx) => {
|
|
1117
|
+
const token = localStorage.getItem('authToken');
|
|
1118
|
+
if (token) {
|
|
1119
|
+
ctx.headers.Authorization = `Bearer ${token}`;
|
|
1120
|
+
}
|
|
1121
|
+
return ctx;
|
|
1122
|
+
},
|
|
1123
|
+
|
|
1124
|
+
// Centralized error handling
|
|
1125
|
+
onError: (error) => {
|
|
1126
|
+
if (error.status === 401) {
|
|
1127
|
+
// Token expired - redirect to login
|
|
1128
|
+
window.location.href = '/login';
|
|
1129
|
+
}
|
|
1130
|
+
if (error.status === 403) {
|
|
1131
|
+
// Access denied - show notification
|
|
1132
|
+
toast.error('Access denied');
|
|
1133
|
+
}
|
|
1134
|
+
if (error.status === 404) {
|
|
1135
|
+
// Not found
|
|
1136
|
+
toast.error('Resource not found');
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
});
|
|
1140
|
+
```
|
|
1141
|
+
|
|
1142
|
+
### Usage (Auth is Automatic)
|
|
1143
|
+
|
|
1144
|
+
```typescript
|
|
1145
|
+
// Auth headers are automatically added to every request
|
|
1146
|
+
const posts = await api.post.list();
|
|
1147
|
+
const user = await api.user.get('123');
|
|
1148
|
+
await api.post.create({ title: 'Hello', authorId: '123' });
|
|
1149
|
+
```
|
|
1150
|
+
|
|
1151
|
+
### Creating Mock JWT Tokens
|
|
1152
|
+
|
|
1153
|
+
For testing different user contexts:
|
|
1154
|
+
|
|
1155
|
+
```typescript
|
|
1156
|
+
import { createMockJwt } from 'schemock/middleware';
|
|
1157
|
+
|
|
1158
|
+
// Create a token for a regular user
|
|
1159
|
+
const userToken = createMockJwt({ userId: 'user-123', role: 'user' });
|
|
1160
|
+
|
|
1161
|
+
// Create a token for an admin (bypasses RLS)
|
|
1162
|
+
const adminToken = createMockJwt({ userId: 'admin-1', role: 'admin' });
|
|
1163
|
+
|
|
1164
|
+
// Set in localStorage or pass directly
|
|
1165
|
+
localStorage.setItem('authToken', userToken);
|
|
1166
|
+
```
|
|
1167
|
+
|
|
1168
|
+
### API Error Handling
|
|
1169
|
+
|
|
1170
|
+
The `ApiError` class provides HTTP-like status codes:
|
|
1171
|
+
|
|
1172
|
+
```typescript
|
|
1173
|
+
import { ApiError } from './generated/client';
|
|
1174
|
+
|
|
1175
|
+
try {
|
|
1176
|
+
await api.post.get('non-existent-id');
|
|
1177
|
+
} catch (error) {
|
|
1178
|
+
if (error instanceof ApiError) {
|
|
1179
|
+
console.log(error.status); // 404
|
|
1180
|
+
console.log(error.code); // "NOT_FOUND"
|
|
1181
|
+
console.log(error.operation); // "post.get"
|
|
1182
|
+
console.log(error.message); // "Post not found: non-existent-id"
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
```
|
|
1186
|
+
|
|
1187
|
+
| Status | Code | When |
|
|
1188
|
+
|--------|------|------|
|
|
1189
|
+
| 403 | `RLS_DENIED` | Row-level security blocked the operation |
|
|
1190
|
+
| 404 | `NOT_FOUND` | Entity not found |
|
|
1191
|
+
| 500 | `INTERNAL_ERROR` | Unexpected error |
|
|
1192
|
+
|
|
1193
|
+
### Simple Usage (No Auth)
|
|
1194
|
+
|
|
1195
|
+
For quick prototyping without auth, use the default client:
|
|
1196
|
+
|
|
1197
|
+
```typescript
|
|
1198
|
+
import { api } from './generated/client';
|
|
1199
|
+
|
|
1200
|
+
// Works immediately - no configuration needed
|
|
1201
|
+
const users = await api.user.list();
|
|
1202
|
+
```
|
|
1203
|
+
|
|
1092
1204
|
## Package Exports
|
|
1093
1205
|
|
|
1094
1206
|
```typescript
|
|
@@ -1142,6 +1254,20 @@ import { defineConfig } from 'schemock/cli';
|
|
|
1142
1254
|
|
|
1143
1255
|
🚧 **In Development** - This project is under active development. APIs may change.
|
|
1144
1256
|
|
|
1257
|
+
## Reporting Issues
|
|
1258
|
+
|
|
1259
|
+
Found a bug or have a feature request?
|
|
1260
|
+
|
|
1261
|
+
**GitHub Issues**: https://github.com/prajyot-tote/schemock/issues
|
|
1262
|
+
|
|
1263
|
+
When reporting bugs, please include:
|
|
1264
|
+
1. Your schema definition (`defineData` calls)
|
|
1265
|
+
2. The command you ran (`npx schemock generate --adapter X`)
|
|
1266
|
+
3. Expected vs actual behavior
|
|
1267
|
+
4. Relevant generated code snippet
|
|
1268
|
+
|
|
1269
|
+
**Important**: If you find a bug in generated code, please report it rather than editing the generated files directly. The fix needs to be in the generator.
|
|
1270
|
+
|
|
1145
1271
|
## License
|
|
1146
1272
|
|
|
1147
1273
|
MIT
|