on-zero 0.1.35 → 0.1.36
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/dist/cjs/createZeroClient.cjs +3 -3
- package/dist/cjs/createZeroClient.js +3 -3
- package/dist/cjs/createZeroClient.js.map +1 -1
- package/dist/cjs/createZeroClient.native.js +3 -3
- package/dist/cjs/createZeroClient.native.js.map +1 -1
- package/dist/cjs/createZeroServer.cjs +42 -29
- package/dist/cjs/createZeroServer.js +34 -21
- package/dist/cjs/createZeroServer.js.map +2 -2
- package/dist/cjs/createZeroServer.native.js +48 -33
- package/dist/cjs/createZeroServer.native.js.map +1 -1
- package/dist/cjs/helpers/getAuth.cjs +31 -0
- package/dist/cjs/helpers/getAuth.js +24 -0
- package/dist/cjs/helpers/getAuth.js.map +6 -0
- package/dist/cjs/helpers/getAuth.native.js +34 -0
- package/dist/cjs/helpers/getAuth.native.js.map +1 -0
- package/dist/cjs/helpers/mutatorContext.cjs +10 -1
- package/dist/cjs/helpers/mutatorContext.js +9 -1
- package/dist/cjs/helpers/mutatorContext.js.map +1 -1
- package/dist/cjs/helpers/mutatorContext.native.js +12 -1
- package/dist/cjs/helpers/mutatorContext.native.js.map +1 -1
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/index.native.js +2 -0
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/where.cjs +2 -2
- package/dist/cjs/where.js +2 -2
- package/dist/cjs/where.js.map +1 -1
- package/dist/cjs/where.native.js +2 -2
- package/dist/cjs/where.native.js.map +1 -1
- package/dist/esm/createZeroClient.js +3 -3
- package/dist/esm/createZeroClient.js.map +1 -1
- package/dist/esm/createZeroClient.mjs +3 -3
- package/dist/esm/createZeroClient.mjs.map +1 -1
- package/dist/esm/createZeroClient.native.js +3 -3
- package/dist/esm/createZeroClient.native.js.map +1 -1
- package/dist/esm/createZeroServer.js +40 -22
- package/dist/esm/createZeroServer.js.map +2 -2
- package/dist/esm/createZeroServer.mjs +43 -30
- package/dist/esm/createZeroServer.mjs.map +1 -1
- package/dist/esm/createZeroServer.native.js +49 -34
- package/dist/esm/createZeroServer.native.js.map +1 -1
- package/dist/esm/helpers/getAuth.js +10 -0
- package/dist/esm/helpers/getAuth.js.map +6 -0
- package/dist/esm/helpers/getAuth.mjs +8 -0
- package/dist/esm/helpers/getAuth.mjs.map +1 -0
- package/dist/esm/helpers/getAuth.native.js +8 -0
- package/dist/esm/helpers/getAuth.native.js.map +1 -0
- package/dist/esm/helpers/mutatorContext.js +9 -1
- package/dist/esm/helpers/mutatorContext.js.map +1 -1
- package/dist/esm/helpers/mutatorContext.mjs +9 -2
- package/dist/esm/helpers/mutatorContext.mjs.map +1 -1
- package/dist/esm/helpers/mutatorContext.native.js +11 -2
- package/dist/esm/helpers/mutatorContext.native.js.map +1 -1
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +2 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/index.native.js +2 -1
- package/dist/esm/index.native.js.map +1 -1
- package/dist/esm/where.js +2 -2
- package/dist/esm/where.js.map +1 -1
- package/dist/esm/where.mjs +2 -2
- package/dist/esm/where.mjs.map +1 -1
- package/dist/esm/where.native.js +2 -2
- package/dist/esm/where.native.js.map +1 -1
- package/package.json +2 -2
- package/readme.md +40 -7
- package/src/createZeroClient.tsx +3 -3
- package/src/createZeroServer.ts +75 -25
- package/src/helpers/{getQueryOrMutatorAuthData.ts → getAuth.ts} +1 -1
- package/src/helpers/mutatorContext.ts +19 -1
- package/src/index.ts +1 -0
- package/src/where.ts +2 -5
- package/types/createZeroServer.d.ts +16 -5
- package/types/createZeroServer.d.ts.map +1 -1
- package/types/helpers/getAuth.d.ts +3 -0
- package/types/helpers/getAuth.d.ts.map +1 -0
- package/types/helpers/mutatorContext.d.ts +3 -1
- package/types/helpers/mutatorContext.d.ts.map +1 -1
- package/types/index.d.ts +1 -0
- package/types/index.d.ts.map +1 -1
- package/types/where.d.ts.map +1 -1
package/readme.md
CHANGED
|
@@ -92,8 +92,7 @@ export const channelById = (props: { channelId: string }) => {
|
|
|
92
92
|
```
|
|
93
93
|
|
|
94
94
|
permissions execute server-side only. on the client they automatically pass. the
|
|
95
|
-
`serverWhere()` helper automatically accesses auth data
|
|
96
|
-
`mutatorContext()` so you don't need to pass it manually.
|
|
95
|
+
`serverWhere()` helper automatically accesses auth data via `getAuth()` so you don't need to pass it manually.
|
|
97
96
|
|
|
98
97
|
## models
|
|
99
98
|
|
|
@@ -176,8 +175,7 @@ export const permissions = serverWhere('channel', (q, auth) => {
|
|
|
176
175
|
})
|
|
177
176
|
```
|
|
178
177
|
|
|
179
|
-
the `serverWhere()` helper automatically gets auth data
|
|
180
|
-
`mutatorContext()`, so you don't manually pass it. permissions only execute
|
|
178
|
+
the `serverWhere()` helper automatically gets auth data via `getAuth()`, so you don't manually pass it. permissions only execute
|
|
181
179
|
server-side - on the client they automatically pass.
|
|
182
180
|
|
|
183
181
|
**for queries:** define permissions inline as a constant in query files:
|
|
@@ -517,6 +515,10 @@ export const zeroServer = createZeroServer({
|
|
|
517
515
|
// - 'mutations': admin bypasses only mutation permissions
|
|
518
516
|
// - 'off': no admin bypass, normal permission checks apply
|
|
519
517
|
defaultAllowAdminRole: 'all',
|
|
518
|
+
|
|
519
|
+
// default authData for zeroServer.mutate when none is provided or in scope
|
|
520
|
+
// defaults to {}
|
|
521
|
+
defaultMutateAuthData: { id: 'system', email: 'system@example.com' },
|
|
520
522
|
})
|
|
521
523
|
```
|
|
522
524
|
|
|
@@ -562,21 +564,52 @@ export const mutate = mutations(schema, permissions, {
|
|
|
562
564
|
|
|
563
565
|
ctx.server?.asyncTasks.push(async () => {
|
|
564
566
|
await ctx.server.actions.indexForSearch(messageId)
|
|
567
|
+
|
|
568
|
+
// zeroServer.mutate works here too - authData is auto-inherited
|
|
569
|
+
await zeroServer.mutate.activity.insert({
|
|
570
|
+
id: randomId(),
|
|
571
|
+
type: 'archive',
|
|
572
|
+
messageId,
|
|
573
|
+
})
|
|
565
574
|
})
|
|
566
575
|
},
|
|
567
576
|
})
|
|
568
577
|
```
|
|
569
578
|
|
|
579
|
+
## getAuth
|
|
580
|
+
|
|
581
|
+
`getAuth()` returns the current user's auth data. works inside both queries and
|
|
582
|
+
mutations:
|
|
583
|
+
|
|
584
|
+
```ts
|
|
585
|
+
import { getAuth } from 'on-zero'
|
|
586
|
+
|
|
587
|
+
const auth = getAuth() // AuthData | null
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
it resolves auth from whichever context is active — mutation context, query
|
|
591
|
+
context, or client-side global. most of the time you won't need this directly
|
|
592
|
+
since `serverWhere()` passes auth to your callback automatically. use `getAuth()`
|
|
593
|
+
when you need auth data outside of those callbacks, like in a shared utility.
|
|
594
|
+
|
|
570
595
|
## patterns
|
|
571
596
|
|
|
572
597
|
**server-only mutations:**
|
|
573
598
|
|
|
574
599
|
```ts
|
|
575
|
-
await zeroServer.mutate(
|
|
576
|
-
|
|
577
|
-
|
|
600
|
+
await zeroServer.mutate.user.insert(user)
|
|
601
|
+
|
|
602
|
+
// with explicit auth (optional - authData auto-resolves from context)
|
|
603
|
+
await zeroServer.mutate.user.insert(user, { id: userId, email })
|
|
578
604
|
```
|
|
579
605
|
|
|
606
|
+
authData is automatically resolved in this order:
|
|
607
|
+
|
|
608
|
+
1. explicit `authData` argument (if passed)
|
|
609
|
+
2. current mutation context (inside a mutation)
|
|
610
|
+
3. auth scope (inside asyncTasks - automatically inherited)
|
|
611
|
+
4. `defaultMutateAuthData` from `createZeroServer` config (defaults to `{}`)
|
|
612
|
+
|
|
580
613
|
**one-off queries with `run()`:**
|
|
581
614
|
|
|
582
615
|
run a query once without subscribing. works on both client and server:
|
package/src/createZeroClient.tsx
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
import { createPermissions } from './createPermissions'
|
|
15
15
|
import { createUseQuery } from './createUseQuery'
|
|
16
16
|
import { createMutators } from './helpers/createMutators'
|
|
17
|
-
import {
|
|
17
|
+
import { getAuth } from './helpers/getAuth'
|
|
18
18
|
import { getAllMutationsPermissions, getMutationsPermissions } from './modelRegistry'
|
|
19
19
|
import { registerQuery } from './queryRegistry'
|
|
20
20
|
import { resolveQuery, type PlainQueryFn } from './resolveQuery'
|
|
@@ -106,7 +106,7 @@ export function createZeroClient<
|
|
|
106
106
|
setEvaluatingPermission(true)
|
|
107
107
|
try {
|
|
108
108
|
return permissionsHelpers.buildPermissionQuery(
|
|
109
|
-
|
|
109
|
+
getAuth(),
|
|
110
110
|
eb,
|
|
111
111
|
rawPerm || ((e: any) => e.and()),
|
|
112
112
|
args.objOrId,
|
|
@@ -130,7 +130,7 @@ export function createZeroClient<
|
|
|
130
130
|
return base
|
|
131
131
|
.where((eb: any) => {
|
|
132
132
|
return permissionsHelpers.buildPermissionQuery(
|
|
133
|
-
|
|
133
|
+
getAuth(),
|
|
134
134
|
eb,
|
|
135
135
|
perm || ((e: any) => e.and()),
|
|
136
136
|
args.objOrId,
|
package/src/createZeroServer.ts
CHANGED
|
@@ -7,7 +7,12 @@ import { Pool } from 'pg'
|
|
|
7
7
|
|
|
8
8
|
import { createPermissions } from './createPermissions'
|
|
9
9
|
import { createMutators } from './helpers/createMutators'
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
getScopedAuthData,
|
|
12
|
+
isInZeroMutation,
|
|
13
|
+
mutatorContext,
|
|
14
|
+
runWithAuthScope,
|
|
15
|
+
} from './helpers/mutatorContext'
|
|
11
16
|
import { runWithQueryContext } from './helpers/queryContext'
|
|
12
17
|
import { getMutationsPermissions } from './modelRegistry'
|
|
13
18
|
import { setCustomQueries } from './run'
|
|
@@ -19,7 +24,7 @@ import type {
|
|
|
19
24
|
AsyncAction,
|
|
20
25
|
AuthData,
|
|
21
26
|
GenericModels,
|
|
22
|
-
|
|
27
|
+
MutatorContext,
|
|
23
28
|
QueryBuilder,
|
|
24
29
|
Transaction,
|
|
25
30
|
} from './types'
|
|
@@ -31,6 +36,19 @@ import type {
|
|
|
31
36
|
} from '@rocicorp/zero'
|
|
32
37
|
import type { TransactionProviderInput } from '@rocicorp/zero/pg'
|
|
33
38
|
|
|
39
|
+
type MutateAuthData = Pick<AuthData, 'email' | 'id'> & Partial<AuthData>
|
|
40
|
+
|
|
41
|
+
type ServerMutate<Models extends GenericModels> = {
|
|
42
|
+
[Key in keyof Models]: {
|
|
43
|
+
[K in keyof Models[Key]['mutate']]: Models[Key]['mutate'][K] extends (
|
|
44
|
+
ctx: MutatorContext,
|
|
45
|
+
arg: infer Arg
|
|
46
|
+
) => any
|
|
47
|
+
? (arg: Arg, authData?: MutateAuthData) => Promise<void>
|
|
48
|
+
: (authData?: MutateAuthData) => Promise<void>
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
34
52
|
export type ValidateQueryArgs = {
|
|
35
53
|
authData: AuthData | null
|
|
36
54
|
queryName: string
|
|
@@ -60,6 +78,7 @@ export function createZeroServer<
|
|
|
60
78
|
validateQuery,
|
|
61
79
|
validateMutation,
|
|
62
80
|
defaultAllowAdminRole = 'all',
|
|
81
|
+
defaultMutateAuthData = {} as MutateAuthData,
|
|
63
82
|
}: {
|
|
64
83
|
/**
|
|
65
84
|
* The DB connection string, same as ZERO_UPSTREAM_DB
|
|
@@ -86,6 +105,12 @@ export function createZeroServer<
|
|
|
86
105
|
* - 'off': admin has no special bypass
|
|
87
106
|
*/
|
|
88
107
|
defaultAllowAdminRole?: AdminRoleMode
|
|
108
|
+
/**
|
|
109
|
+
* Default authData used by zeroServer.mutate when no authData is provided
|
|
110
|
+
* and none is available from mutation context or auth scope.
|
|
111
|
+
* Defaults to {}.
|
|
112
|
+
*/
|
|
113
|
+
defaultMutateAuthData?: MutateAuthData
|
|
89
114
|
}) {
|
|
90
115
|
setSchema(schema)
|
|
91
116
|
|
|
@@ -139,7 +164,7 @@ export function createZeroServer<
|
|
|
139
164
|
if (!skipAsyncTasks && asyncTasks.length) {
|
|
140
165
|
const id = randomId()
|
|
141
166
|
console.info(`[push] complete, running async tasks ${asyncTasks.length} id ${id}`)
|
|
142
|
-
Promise.all(asyncTasks.map((task) => task
|
|
167
|
+
Promise.all(asyncTasks.map((task) => runWithAuthScope(authData, task)))
|
|
143
168
|
.then(() => {
|
|
144
169
|
console.info(`[push] async tasks complete ${id}`)
|
|
145
170
|
})
|
|
@@ -214,11 +239,20 @@ export function createZeroServer<
|
|
|
214
239
|
}
|
|
215
240
|
}
|
|
216
241
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
242
|
+
async function runMutate(
|
|
243
|
+
modelName: string,
|
|
244
|
+
mutatorName: string,
|
|
245
|
+
mutatorArg: unknown,
|
|
246
|
+
authData?: MutateAuthData
|
|
247
|
+
) {
|
|
248
|
+
// auto-resolve authData from mutation context or auth scope
|
|
249
|
+
if (!authData) {
|
|
250
|
+
const scoped = getScopedAuthData()
|
|
251
|
+
if (scoped) {
|
|
252
|
+
authData = scoped as MutateAuthData
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
222
256
|
const asyncTasks: Array<() => Promise<void>> = []
|
|
223
257
|
|
|
224
258
|
const mutators = createMutators({
|
|
@@ -226,9 +260,7 @@ export function createZeroServer<
|
|
|
226
260
|
environment: 'server',
|
|
227
261
|
asyncTasks,
|
|
228
262
|
authData: {
|
|
229
|
-
|
|
230
|
-
email: 'admin@start.chat',
|
|
231
|
-
role: 'admin',
|
|
263
|
+
...defaultMutateAuthData,
|
|
232
264
|
...authData,
|
|
233
265
|
},
|
|
234
266
|
createServerActions,
|
|
@@ -236,27 +268,45 @@ export function createZeroServer<
|
|
|
236
268
|
validateMutation,
|
|
237
269
|
})
|
|
238
270
|
|
|
271
|
+
const modelMutators = mutators[modelName as keyof typeof mutators] as Record<
|
|
272
|
+
string,
|
|
273
|
+
(tx: Transaction, arg?: unknown) => Promise<void>
|
|
274
|
+
>
|
|
275
|
+
const mutator = modelMutators[mutatorName]
|
|
276
|
+
|
|
239
277
|
await transaction(async (tx) => {
|
|
240
|
-
await
|
|
278
|
+
await mutator(tx, mutatorArg)
|
|
241
279
|
})
|
|
242
280
|
|
|
243
281
|
if (asyncTasks.length) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
console.error(`[mutate] error: async tasks failed`, err)
|
|
255
|
-
})
|
|
256
|
-
}
|
|
282
|
+
const id = randomId()
|
|
283
|
+
const resolvedAuth = authData ?? null
|
|
284
|
+
console.info(`[mutate] running async tasks ${asyncTasks.length} id ${id}`)
|
|
285
|
+
Promise.all(asyncTasks.map((t) => runWithAuthScope(resolvedAuth, t)))
|
|
286
|
+
.then(() => {
|
|
287
|
+
console.info(`[mutate] async tasks complete ${id}`)
|
|
288
|
+
})
|
|
289
|
+
.catch((err) => {
|
|
290
|
+
console.error(`[mutate] error: async tasks failed`, err)
|
|
291
|
+
})
|
|
257
292
|
}
|
|
258
293
|
}
|
|
259
294
|
|
|
295
|
+
// zeroServer.mutate.user.insert(user)
|
|
296
|
+
const mutate = new Proxy({} as ServerMutate<Models>, {
|
|
297
|
+
get(_, modelName: string) {
|
|
298
|
+
return new Proxy(
|
|
299
|
+
{},
|
|
300
|
+
{
|
|
301
|
+
get(_, mutatorName: string) {
|
|
302
|
+
return (arg: unknown, authData?: MutateAuthData) =>
|
|
303
|
+
runMutate(modelName, mutatorName, arg, authData)
|
|
304
|
+
},
|
|
305
|
+
}
|
|
306
|
+
)
|
|
307
|
+
},
|
|
308
|
+
})
|
|
309
|
+
|
|
260
310
|
async function transaction<
|
|
261
311
|
CB extends (tx: Transaction) => Promise<any>,
|
|
262
312
|
Returns extends CB extends (tx: Transaction) => Promise<infer X> ? X : never,
|
|
@@ -4,7 +4,7 @@ import { isInQueryContext, queryAuthData } from './queryContext'
|
|
|
4
4
|
|
|
5
5
|
import type { AuthData } from '../types'
|
|
6
6
|
|
|
7
|
-
export function
|
|
7
|
+
export function getAuth(): AuthData | null {
|
|
8
8
|
if (isInZeroMutation()) {
|
|
9
9
|
return mutatorContext().authData as AuthData
|
|
10
10
|
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { createAsyncContext } from '@take-out/helpers'
|
|
2
2
|
|
|
3
|
-
import type { MutatorContext } from '../types'
|
|
3
|
+
import type { AuthData, MutatorContext } from '../types'
|
|
4
4
|
|
|
5
5
|
const asyncContext = createAsyncContext<MutatorContext>()
|
|
6
6
|
|
|
7
|
+
// lightweight auth-only scope for asyncTasks (where mutation context is gone but authData is needed)
|
|
8
|
+
const authScopeContext = createAsyncContext<AuthData | null>()
|
|
9
|
+
|
|
7
10
|
export function mutatorContext(): MutatorContext {
|
|
8
11
|
const currentContext = asyncContext.get()
|
|
9
12
|
if (!currentContext) {
|
|
@@ -23,3 +26,18 @@ export function runWithContext<T>(
|
|
|
23
26
|
): Promise<T> {
|
|
24
27
|
return asyncContext.run(context, fn)
|
|
25
28
|
}
|
|
29
|
+
|
|
30
|
+
// auto-resolve authData from mutation context or auth scope
|
|
31
|
+
export function getScopedAuthData(): AuthData | null | undefined {
|
|
32
|
+
if (isInZeroMutation()) {
|
|
33
|
+
return mutatorContext().authData
|
|
34
|
+
}
|
|
35
|
+
return authScopeContext.get() ?? undefined
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function runWithAuthScope<T>(
|
|
39
|
+
authData: AuthData | null,
|
|
40
|
+
fn: () => T | Promise<T>
|
|
41
|
+
): Promise<T> {
|
|
42
|
+
return authScopeContext.run(authData, fn)
|
|
43
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ export * from './helpers/batchQuery'
|
|
|
4
4
|
export * from './helpers/createMutators'
|
|
5
5
|
export * from './helpers/ensureLoggedIn'
|
|
6
6
|
export * from './helpers/mutatorContext'
|
|
7
|
+
export { getAuth } from './helpers/getAuth'
|
|
7
8
|
|
|
8
9
|
export * from './createZeroClient'
|
|
9
10
|
export * from './createUseQuery'
|
package/src/where.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { globalValue } from '@take-out/helpers'
|
|
2
2
|
|
|
3
3
|
import { isServer } from './constants'
|
|
4
|
-
import {
|
|
4
|
+
import { getAuth } from './helpers/getAuth'
|
|
5
5
|
|
|
6
6
|
import type { TableName, Where } from './types'
|
|
7
7
|
import type { Condition, ExpressionBuilder } from '@rocicorp/zero'
|
|
@@ -31,10 +31,7 @@ export function where<Table extends TableName, Builder extends Where<Table>>(
|
|
|
31
31
|
): Where<Table, any> | Builder {
|
|
32
32
|
const whereFn = (b || a) as any
|
|
33
33
|
|
|
34
|
-
const wrappedWhereFn = ((
|
|
35
|
-
a: ExpressionBuilder<any, any>,
|
|
36
|
-
b = getQueryOrMutatorAuthData()
|
|
37
|
-
) => {
|
|
34
|
+
const wrappedWhereFn = ((a: ExpressionBuilder<any, any>, b = getAuth()) => {
|
|
38
35
|
if (!isServer && isServerOnly && !_evaluatingPermission) {
|
|
39
36
|
// on client (web or native) where conditions always pass
|
|
40
37
|
return a.and()
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import type { AdminRoleMode, AsyncAction, AuthData, GenericModels,
|
|
1
|
+
import type { AdminRoleMode, AsyncAction, AuthData, GenericModels, MutatorContext, QueryBuilder, Transaction } from './types';
|
|
2
2
|
import type { AnyQueryRegistry, HumanReadable, Query, Schema as ZeroSchema } from '@rocicorp/zero';
|
|
3
|
+
type MutateAuthData = Pick<AuthData, 'email' | 'id'> & Partial<AuthData>;
|
|
4
|
+
type ServerMutate<Models extends GenericModels> = {
|
|
5
|
+
[Key in keyof Models]: {
|
|
6
|
+
[K in keyof Models[Key]['mutate']]: Models[Key]['mutate'][K] extends (ctx: MutatorContext, arg: infer Arg) => any ? (arg: Arg, authData?: MutateAuthData) => Promise<void> : (authData?: MutateAuthData) => Promise<void>;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
3
9
|
export type ValidateQueryArgs = {
|
|
4
10
|
authData: AuthData | null;
|
|
5
11
|
queryName: string;
|
|
@@ -13,7 +19,7 @@ export type ValidateMutationArgs = {
|
|
|
13
19
|
};
|
|
14
20
|
export type ValidateQueryFn = (args: ValidateQueryArgs) => void;
|
|
15
21
|
export type ValidateMutationFn = (args: ValidateMutationArgs) => void | Promise<void>;
|
|
16
|
-
export declare function createZeroServer<Schema extends ZeroSchema, Models extends GenericModels, ServerActions extends Record<string, unknown>>({ createServerActions, database, schema, models, queries, validateQuery, validateMutation, defaultAllowAdminRole, }: {
|
|
22
|
+
export declare function createZeroServer<Schema extends ZeroSchema, Models extends GenericModels, ServerActions extends Record<string, unknown>>({ createServerActions, database, schema, models, queries, validateQuery, validateMutation, defaultAllowAdminRole, defaultMutateAuthData, }: {
|
|
17
23
|
/**
|
|
18
24
|
* The DB connection string, same as ZERO_UPSTREAM_DB
|
|
19
25
|
*/
|
|
@@ -39,6 +45,12 @@ export declare function createZeroServer<Schema extends ZeroSchema, Models exten
|
|
|
39
45
|
* - 'off': admin has no special bypass
|
|
40
46
|
*/
|
|
41
47
|
defaultAllowAdminRole?: AdminRoleMode;
|
|
48
|
+
/**
|
|
49
|
+
* Default authData used by zeroServer.mutate when no authData is provided
|
|
50
|
+
* and none is available from mutation context or auth scope.
|
|
51
|
+
* Defaults to {}.
|
|
52
|
+
*/
|
|
53
|
+
defaultMutateAuthData?: MutateAuthData;
|
|
42
54
|
}): {
|
|
43
55
|
handleMutationRequest: ({ authData, request, skipAsyncTasks, }: {
|
|
44
56
|
authData: AuthData | null;
|
|
@@ -170,9 +182,8 @@ export declare function createZeroServer<Schema extends ZeroSchema, Models exten
|
|
|
170
182
|
}];
|
|
171
183
|
}>;
|
|
172
184
|
transaction: <CB extends (tx: Transaction) => Promise<any>, Returns extends CB extends (tx: Transaction) => Promise<infer X> ? X : never>(query: CB) => Promise<Returns>;
|
|
173
|
-
mutate:
|
|
174
|
-
awaitAsyncTasks?: boolean;
|
|
175
|
-
}) => Promise<void>;
|
|
185
|
+
mutate: ServerMutate<Models>;
|
|
176
186
|
query: <R>(cb: (q: QueryBuilder) => Query<any, Schema, R>, authData?: AuthData | null) => Promise<HumanReadable<R>>;
|
|
177
187
|
};
|
|
188
|
+
export {};
|
|
178
189
|
//# sourceMappingURL=createZeroServer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createZeroServer.d.ts","sourceRoot":"","sources":["../src/createZeroServer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createZeroServer.d.ts","sourceRoot":"","sources":["../src/createZeroServer.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EACX,QAAQ,EACR,aAAa,EACb,cAAc,EACd,YAAY,EACZ,WAAW,EACZ,MAAM,SAAS,CAAA;AAChB,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,KAAK,EACL,MAAM,IAAI,UAAU,EACrB,MAAM,gBAAgB,CAAA;AAGvB,KAAK,cAAc,GAAG,IAAI,CAAC,QAAQ,EAAE,OAAO,GAAG,IAAI,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;AAExE,KAAK,YAAY,CAAC,MAAM,SAAS,aAAa,IAAI;KAC/C,GAAG,IAAI,MAAM,MAAM,GAAG;SACpB,CAAC,IAAI,MAAM,MAAM,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CACnE,GAAG,EAAE,cAAc,EACnB,GAAG,EAAE,MAAM,GAAG,KACX,GAAG,GACJ,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,GACtD,CAAC,QAAQ,CAAC,EAAE,cAAc,KAAK,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QAAQ,GAAG,IAAI,CAAA;QACzB,OAAO,EAAE,OAAO,CAAA;QAChB,cAAc,CAAC,EAAE,OAAO,CAAA;KACzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iDAuCE;QACD,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAA;QACzB,OAAO,EAAE,OAAO,CAAA;KACjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBA2HC,EAAE,SAAS,CAAC,EAAE,EAAE,WAAW,KAAK,OAAO,CAAC,GAAG,CAAC,EAC5C,OAAO,SAAS,EAAE,SAAS,CAAC,EAAE,EAAE,WAAW,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,SACrE,EAAE,KAAG,OAAO,CAAC,OAAO,CAAC;;YAef,CAAC,MACV,CAAC,CAAC,EAAE,YAAY,KAAK,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC,aACnC,QAAQ,GAAG,IAAI,KACzB,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;EAuC7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getAuth.d.ts","sourceRoot":"","sources":["../../src/helpers/getAuth.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAExC,wBAAgB,OAAO,IAAI,QAAQ,GAAG,IAAI,CASzC"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import type { MutatorContext } from '../types';
|
|
1
|
+
import type { AuthData, MutatorContext } from '../types';
|
|
2
2
|
export declare function mutatorContext(): MutatorContext;
|
|
3
3
|
export declare function isInZeroMutation(): boolean;
|
|
4
4
|
export declare function runWithContext<T>(context: MutatorContext, fn: () => T | Promise<T>): Promise<T>;
|
|
5
|
+
export declare function getScopedAuthData(): AuthData | null | undefined;
|
|
6
|
+
export declare function runWithAuthScope<T>(authData: AuthData | null, fn: () => T | Promise<T>): Promise<T>;
|
|
5
7
|
//# sourceMappingURL=mutatorContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutatorContext.d.ts","sourceRoot":"","sources":["../../src/helpers/mutatorContext.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"mutatorContext.d.ts","sourceRoot":"","sources":["../../src/helpers/mutatorContext.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAOxD,wBAAgB,cAAc,IAAI,cAAc,CAO/C;AAED,wBAAgB,gBAAgB,YAE/B;AAED,wBAAgB,cAAc,CAAC,CAAC,EAC9B,OAAO,EAAE,cAAc,EACvB,EAAE,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GACvB,OAAO,CAAC,CAAC,CAAC,CAEZ;AAGD,wBAAgB,iBAAiB,IAAI,QAAQ,GAAG,IAAI,GAAG,SAAS,CAK/D;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,QAAQ,EAAE,QAAQ,GAAG,IAAI,EACzB,EAAE,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GACvB,OAAO,CAAC,CAAC,CAAC,CAEZ"}
|
package/types/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export * from './helpers/batchQuery';
|
|
|
4
4
|
export * from './helpers/createMutators';
|
|
5
5
|
export * from './helpers/ensureLoggedIn';
|
|
6
6
|
export * from './helpers/mutatorContext';
|
|
7
|
+
export { getAuth } from './helpers/getAuth';
|
|
7
8
|
export * from './createZeroClient';
|
|
8
9
|
export * from './createUseQuery';
|
|
9
10
|
export * from './resolveQuery';
|
package/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA;AACpC,cAAc,0BAA0B,CAAA;AACxC,cAAc,0BAA0B,CAAA;AACxC,cAAc,0BAA0B,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA;AACpC,cAAc,0BAA0B,CAAA;AACxC,cAAc,0BAA0B,CAAA;AACxC,cAAc,0BAA0B,CAAA;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAE3C,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,OAAO,CAAA;AACrB,OAAO,EAAE,SAAS,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAA;AACzD,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,eAAe,CAAA;AAC7B,cAAc,OAAO,CAAA;AAErB,mBAAmB,SAAS,CAAA"}
|
package/types/where.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"where.d.ts","sourceRoot":"","sources":["../src/where.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAqB,MAAM,gBAAgB,CAAA;AAMlE,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,QAErD;AAED,wBAAgB,KAAK,CAAC,KAAK,SAAS,SAAS,EAAE,OAAO,SAAS,KAAK,CAAC,KAAK,CAAC,EACzE,SAAS,EAAE,KAAK,EAChB,OAAO,EAAE,OAA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