Skip to content

Commit 52334b5

Browse files
committed
chore: more jsdoc
1 parent b75329b commit 52334b5

File tree

8 files changed

+190
-2
lines changed

8 files changed

+190
-2
lines changed

packages/core/src/factory/execute.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { nanoid } from 'nanoid'
12
import type { ResourceFactory, ResourceFactoryContext, ResourceFactoryFn } from '../types/factory.js'
23
import type { Awaitable } from '../util/types.js'
34
import { createResourceInstanceReference } from '../resource/resourceReference.js'
@@ -25,6 +26,7 @@ export async function executeFactory(mq: MoquerieInstance, factory: ResourceFact
2526
db: ctx.db,
2627
repeat: repeat.bind(null, result) as (fn: (item: any) => any, min: number, max: number) => Promise<any[]>,
2728
pickRandom,
29+
generateId: nanoid,
2830
}
2931

3032
const rawResult = fn(factoryContext)

packages/core/src/rest/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function setupRestApi(mq: MoquerieInstance, expressApp: Application
5959
db: (ctx.db as UntypedQueryManagerProxy),
6060
pubsub: ctx.pubSubs,
6161
faker: await getFaker(mq),
62-
generateId: () => nanoid(),
62+
generateId: nanoid,
6363
generateResource: async (resourceName, factoryId, count = 1) => {
6464
const resourceType = ctx.schema.types[resourceName]
6565
if (!resourceType) {

packages/core/src/types/apiRoute.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,65 @@ export interface ApiRouter {
1919
export type DefineApiRouteSetupFn = (router: ApiRouter) => void
2020

2121
export interface ApiRouteContext {
22+
/**
23+
* The database query manager.
24+
*/
2225
db: QueryManagerProxy
26+
/**
27+
* The pubsub instance to send real-time updates.
28+
*/
2329
pubsub: PubSubs
30+
/**
31+
* Generate a random id.
32+
*/
2433
generateId: () => string
34+
/**
35+
* Generate one or more resource instances using a factory.
36+
*/
2537
generateResource: (resourceName: string, factoryId: string, count?: number) => Promise<ResourceInstanceReference[]>
38+
/**
39+
* The faker instance.
40+
*/
2641
faker: Faker
42+
/**
43+
* Repeat a function multiple times.
44+
*/
2745
repeat: <T = any>(fn: () => T, min: number, max: number) => Promise<Array<T>>
46+
/**
47+
* Pick a random item from a list.
48+
*/
2849
pickRandom: <T extends string | number | boolean = any>(list: T[]) => T | null
50+
/**
51+
* The current request.
52+
*/
2953
request: RequestLike
54+
/**
55+
* The path parameters.
56+
*
57+
* Example:
58+
*
59+
* For the route `/users/:id`, if the URL is `/users/123`, then `params` will be `{ id: '123' }`.
60+
*/
3061
params: Record<string, string>
62+
/**
63+
* The query parameters.
64+
*
65+
* Example:
66+
*
67+
* For the URL `/users?name=John`, then `query` will be `{ name: 'John' }`.
68+
*/
3169
query: Record<string, string>
70+
/**
71+
* Read the request body.
72+
*/
3273
readBody: () => Promise<any>
74+
/**
75+
* Set the response type.
76+
*/
3377
setResponseType: (type: string) => void
78+
/**
79+
* Create an HTTP error.
80+
*/
3481
createError: (message: string, data?: any) => Error
3582
}
3683

packages/core/src/types/factory.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,26 @@ export interface ResourceFactory {
149149
}
150150

151151
export interface ResourceFactoryContext {
152+
/**
153+
* The faker instance.
154+
*/
152155
faker: Faker
156+
/**
157+
* The database query manager.
158+
*/
153159
db: QueryManagerProxy
160+
/**
161+
* Repeat a function multiple times.
162+
*/
154163
repeat: <T = any>(fn: (context: ResourceFactoryLazyContext) => T, min: number, max: number) => Promise<Array<T>>
164+
/**
165+
* Pick a random item from a list.
166+
*/
155167
pickRandom: <T extends string | number | boolean = any>(list: T[]) => T | null
168+
/**
169+
* Generate a random id.
170+
*/
171+
generateId: () => string
156172
}
157173

158174
export interface ResourceFactoryLazyContext<TItem = any> {

packages/core/src/types/resolver.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,27 @@ export interface Resolver {
99
}
1010

1111
export interface ResolverContext {
12+
/**
13+
* The parent object. For example, if the resolver is for a field `user` on a `Post` type, then `parent` will be the `Post` object.
14+
*/
1215
parent: any
16+
/**
17+
* The input object. This is the object that was passed to the resolver as part of the field parameters.
18+
*
19+
* For example, if the resolver is for a field `user` on a `Post` type, then `input` will be the parameters object that was passed to the `user` field.
20+
*/
1321
input: any
22+
/**
23+
* The database query manager.
24+
*/
1425
db: QueryManagerProxy
26+
/**
27+
* The pubsub instance to send real-time updates.
28+
*/
1529
pubsub: PubSubs
30+
/**
31+
* Generate a random id.
32+
*/
1633
generateId: () => string
1734
}
1835

packages/core/src/types/script.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,33 @@ import type { Awaitable } from '../util/types.js'
55
import type { ResourceInstanceReference } from './resource.js'
66

77
export interface ScriptContext {
8+
/**
9+
* The database query manager.
10+
*/
811
db: QueryManagerProxy
12+
/**
13+
* The pubsub instance to send real-time updates.
14+
*/
915
pubsub: PubSubs
16+
/**
17+
* Generate a random id.
18+
*/
1019
generateId: () => string
20+
/**
21+
* Generate one or more resource instances using a factory.
22+
*/
1123
generateResource: (resourceName: string, factoryId: string, count?: number) => Promise<ResourceInstanceReference[]>
24+
/**
25+
* The faker instance.
26+
*/
1227
faker: Faker
28+
/**
29+
* Repeat a function multiple times.
30+
*/
1331
repeat: <T = any>(fn: () => T, min: number, max: number) => Promise<Array<T>>
32+
/**
33+
* Pick a random item from a list.
34+
*/
1435
pickRandom: <T extends string | number | boolean = any>(list: T[]) => T | null
1536
}
1637

packages/moquerie/src/mocks.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ export function defineResolvers<TResolvers extends ResolverBaseDefinitions>(reso
5656
}
5757
}
5858

59+
/**
60+
* Modify the resource schema, for example to add internal fields that are not exposed in the API.
61+
* @example
62+
*
63+
* ```ts
64+
import { defineSchemaTransforms } from 'moquerie/mocks'
65+
66+
export default {
67+
// Use a spread operator to be able to use other functions like `defineResolvers` or `defineScripts`
68+
...defineSchemaTransforms(({ schema, createEnumField }) => {
69+
// Add a new field to the `User` type
70+
schema.types.User.fields.customInternalField = createEnumField('customInternalField', [
71+
{ value: 1, description: 'One' },
72+
{ value: 2, description: 'Two' },
73+
{ value: 3, description: 'Three' },
74+
])
75+
}),
76+
}
77+
```
78+
*/
5979
export function defineSchemaTransforms(handlers: SchemaTransformAction | Array<SchemaTransformAction>) {
6080
return {
6181
__schemaTransforms: handlers,
@@ -64,9 +84,44 @@ export function defineSchemaTransforms(handlers: SchemaTransformAction | Array<S
6484

6585
export type ScriptOption = ScriptFn | {
6686
description?: string
87+
/**
88+
* The script function.
89+
*/
6790
fn: ScriptFn
6891
}
6992

93+
/**
94+
* Add scripts to create more complex scenarios using multiple factories or other actions.
95+
* You can then call these scripts in the dashboard.
96+
*
97+
* @example
98+
*
99+
* ```ts
100+
import { defineScripts } from 'moquerie/mocks'
101+
102+
export default {
103+
// Use a spread operator to be able to use other functions like `defineResolvers` or `defineSchemaTransforms`
104+
...defineScripts({
105+
createSimpleMessage: {
106+
description: `Create a simple message sent by current user`,
107+
fn: async ({ generateResource, db }) => {
108+
// Create message
109+
const [ref] = await generateResource('Message', 'SimpleMessageFactory')
110+
111+
// Update message with current user
112+
const me = await db.User.findFirstReference((data, { tags }) => tags.includes('me'))
113+
if (!me) {
114+
throw new Error(`User with tag 'me' not found`)
115+
}
116+
await db.Message.updateFirst({
117+
from: me,
118+
}, (_, instance) => instance.id === ref.__id)
119+
},
120+
},
121+
}),
122+
}
123+
```
124+
*/
70125
export function defineScripts(scripts: Record<string, ScriptOption>) {
71126
const list: Array<Omit<ScriptItem, 'file'>> = []
72127
for (const id in scripts) {
@@ -79,12 +134,36 @@ export function defineScripts(scripts: Record<string, ScriptOption>) {
79134
}
80135
}
81136

137+
/**
138+
* Define API routes to be used in the dashboard. Use the `router` parameter to add new routes answering different HTTP verbs. Inside the route handler, return the response.
139+
*
140+
* You can define parameters in the route path using the `:name` syntax (see https://www.npmjs.com/package/path-to-regexp).
141+
*
142+
* @example
143+
*
144+
* ```ts
145+
import { defineApiRoutes } from 'moquerie/mocks'
146+
147+
export default {
148+
// Use a spread operator to be able to use other functions like `defineResolvers` or `defineScripts`
149+
...defineApiRoutes((router) => {
150+
// Add a new route
151+
router.get('/messages/count', async ({ db }) => {
152+
return (await db.Message.findMany()).length
153+
})
154+
}),
155+
}
156+
```
157+
*/
82158
export function defineApiRoutes(fn: DefineApiRouteSetupFn) {
83159
return {
84160
__apiRouteFn: fn,
85161
}
86162
}
87163

164+
/**
165+
* Define a factory to generate resources. If you need more complex logic, create factories for the basic generation then use `defineScripts` to create scripts.
166+
*/
88167
export function defineFactory<TInfo extends Partial<ResourceFactoryInfo>, TFn extends ResourceFactoryFn>(info: TInfo, fn: TFn) {
89168
return {
90169
info,

playground/src/schema.mock.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineResolvers, defineSchemaTransforms, defineScripts } from 'moquerie/mocks'
1+
import { defineApiRoutes, defineResolvers, defineSchemaTransforms, defineScripts } from 'moquerie/mocks'
22

33
export default {
44
...defineResolvers({
@@ -96,4 +96,10 @@ export default {
9696
},
9797
},
9898
}),
99+
100+
...defineApiRoutes((router) => {
101+
router.get('/hello', async ({ db }) => {
102+
return (await db.Message.findMany()).length
103+
})
104+
}),
99105
}

0 commit comments

Comments
 (0)