Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5b44d37

Browse files
committed
feat: remove ctx first param and add "raw" fn to action
1 parent9e0a541 commit5b44d37

File tree

3 files changed

+43
-45
lines changed

3 files changed

+43
-45
lines changed

‎packages/action/src/action.ts‎

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
/* eslint-disable no-unused-vars */
2-
import{
3-
z,
4-
// infer as _infer,
5-
ZodObject,
6-
ZodRawShape,
7-
ZodSchema,
8-
ZodTypeAny,
9-
ZodType,
10-
}from'zod';
2+
import{z,ZodObject,ZodRawShape,ZodSchema,ZodTypeAny,ZodType}from'zod';
113
import{Simplify,zInfer}from'./utils/type-utils';
124

135
// Generic type for resolver functions
@@ -20,8 +12,6 @@ export type Resolver<
2012
ctx:Simplify<TContext>;
2113
})=>Promise<TOutputSchemaextendsZodTypeAny ?zInfer<TOutputSchema> :void>;
2214

23-
// Define the builder interface capturing generic types for input and output
24-
2515
exporttypeZodSchemaOrRawShape=ZodSchema<any>|ZodRawShape;
2616
exporttypeInferZodSchemaOrRawShape<TextendsZodSchemaOrRawShape>=
2717
TextendsZodRawShape ?ZodObject<T> :T;
@@ -117,7 +107,7 @@ export function action<
117107
resolver,
118108
type:'mutation',
119109
};
120-
returncreateResolver(newDef)asunknownasAction<
110+
returncreateAction(newDef)asunknownasAction<
121111
TResolver,
122112
TInputSchema,
123113
TOutputSchema,
@@ -143,7 +133,7 @@ export function action<
143133
resolver,
144134
type:'query',
145135
};
146-
returncreateResolver(newDef)asunknownasAction<
136+
returncreateAction(newDef)asunknownasAction<
147137
TResolver,
148138
TInputSchema,
149139
TOutputSchema,
@@ -158,6 +148,7 @@ export function action<
158148
...initialDef,
159149
});
160150
}
151+
161152
exportinterfaceActionBuilder<
162153
TInputSchemaextendsZodTypeAny|undefined,
163154
TOutputSchemaextendsZodTypeAny|undefined,
@@ -194,17 +185,17 @@ export type Action<
194185
TTypeextends'mutation'|'query'|undefined,
195186
TContextextendsRecord<string,any>|unknown=unknown,
196187
>=ActionDef<TResolver,TInputSchema,TOutputSchema,TType,TContext>&{
197-
callerWithoutParser:(
188+
raw:(
198189
ctx:TContext,
199190
input:TInputSchemaextendsZodTypeAny ?zInfer<TInputSchema> :void
200-
)=>ReturnType<TResolver>;
201-
}&{
202-
(
203-
ctx:TContext,
191+
)=>Simplify<ReturnType<TResolver>>;
192+
}&((
204193
input:TInputSchemaextendsZodTypeAny ?zInfer<TInputSchema> :void
205-
):ReturnType<TResolver>;
206-
};
207-
exportfunctioncreateResolver<
194+
)=>ReturnType<TResolver>);
195+
196+
//
197+
198+
exportfunctioncreateAction<
208199
TResolverextendsResolver<any,any,any>,
209200
TInputSchemaextendsZodTypeAny|undefined,
210201
TOutputSchemaextendsZodTypeAny|undefined,
@@ -214,7 +205,7 @@ export function createResolver<
214205
/**
215206
* Invokes the resolver with middleware logic
216207
*/
217-
constcallerWithMiddleware=async(
208+
constinvokeWithMiddleware=async(
218209
ctx:TContext,
219210
input:TInputSchemaextendsZodTypeAny ?zInfer<TInputSchema> :null,
220211
middlewares:Middleware<TContext,TContext>[]=def.middleware
@@ -240,31 +231,34 @@ export function createResolver<
240231
};
241232

242233
/**
243-
*Calls the resolver function without parsing input/output
244-
* Useful forcalling the resolver whentheinput/output is already parsed
234+
*Invokes the resolver without parsing input/output
235+
* Useful forraw calls fromthebackend
245236
*/
246-
constcallerWithoutParser=async(
237+
constrawCall=async(
247238
ctx:TContext,
248239
input:TInputSchemaextendsZodTypeAny ?zInfer<TInputSchema> :null
249240
)=>{
250-
returncallerWithMiddleware(ctx,input);
241+
returninvokeWithMiddleware(ctx,input);
251242
};
252243

253244
/**
254245
* Invokes the resolver with parsing input/output and middleware logic
255-
* Useful for safecallingtheresolver directly
246+
* Useful for safecalls fromthefrontend
256247
*/
257-
constcallerWithParser=async(ctx:TContext,input:any)=>{
248+
constsafeCall=async(input:any)=>{
258249
constmaybeParsedInput=def.inputSchema
259250
?def.inputSchema.parse(input)
260251
:input;
261-
constresult=awaitcallerWithMiddleware(ctx,maybeParsedInput);
252+
constresult=awaitinvokeWithMiddleware(
253+
undefinedasany,
254+
maybeParsedInput
255+
);
262256

263257
constmaybeParsedOutput=def.outputSchema
264258
?def.outputSchema.parse(result)
265259
:result;
266260
returnmaybeParsedOutput;
267261
};
268262

269-
returnObject.assign(callerWithParser,def,{callerWithoutParser});
263+
returnObject.assign(safeCall,def,{raw:rawCall});
270264
}

‎packages/action/test/action.test.ts‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,22 @@ const d = {
2424
},
2525
};
2626

27-
typeApiContext={
27+
typePublicActionCtx={
2828
user?:{id:string};
2929
};
3030

31-
constpublicAction=action<ApiContext>();
31+
typeAuthedActionCtx={
32+
user:{id:string};
33+
};
34+
35+
constpublicAction=action<PublicActionCtx>();
3236

33-
constauthedAction=action<Required<ApiContext>>().use(
34-
async({ ctx, next})=>{
35-
if(!ctx.user){
36-
thrownewError('No user');
37-
}
38-
returnawaitnext();
37+
constauthedAction=action<AuthedActionCtx>().use(async({ ctx, next})=>{
38+
if(!ctx.user){
39+
thrownewError('No user');
3940
}
40-
);
41+
returnawaitnext();
42+
});
4143

4244
describe('Action',()=>{
4345
expect(false).toBe(false);
@@ -118,8 +120,8 @@ describe('Action', () => {
118120
constcreateUser=action()
119121
.input({name:z.string()})
120122
.output(z.boolean())
121-
.mutation(async({ input})=>{
122-
returninput.name==='test123';
123+
.mutation(async({ input, ctx})=>{
124+
returninput?.name==='test123';
123125
});
124126

125127
constresult=awaitcreateUser.raw(d.ctx,{name:'test123'});
@@ -128,10 +130,13 @@ describe('Action', () => {
128130
constresult2=awaitcreateUser.raw(d.ctx,{name:'test'});
129131
expect(result2).toBe(false);
130132

133+
// These should not throw errors since .raw doesn't parse inputs
134+
awaitexpect(
135+
//@ts-expect-error
136+
createUser.raw(d.ctx,{name:1})
137+
).resolves.not.toThrowError();
131138
//@ts-expect-error
132-
expect(()=>createUser.raw(d.ctx,{name:1})).rejects.toThrowError();
133-
//@ts-expect-error
134-
expect(()=>createUser.raw(d.ctx)).rejects.toThrowError();
139+
awaitexpect(createUser.raw(d.ctx)).resolves.not.toThrowError();
135140

136141
constinputSchema=createUser.inputSchema;
137142
});
@@ -217,7 +222,6 @@ describe('Action', () => {
217222
});
218223

219224
awaitexpect(async()=>{
220-
//@ts-expect-error
221225
awaitcreateUser({name:'test'});
222226
}).rejects.toThrowError();
223227

File renamed without changes.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp