Skip to content

Commit a2e6728

Browse files
committed
Support partial success cases
1 parent 78b0715 commit a2e6728

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/hook-file.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class HookFile extends ModuleBuilder {
7878
const PageParam = () => this.runtime.type('PageParam');
7979
const QueryError = () => this.runtime.type('QueryError');
8080
const assert = () => this.runtime.fn('assert');
81+
const isError = () => this.runtime.fn('isError');
8182

8283
const type = (t: string) => this.types.type(t);
8384

@@ -156,7 +157,7 @@ export class HookFile extends ModuleBuilder {
156157
yield ` const res = await ${guard()}(${serviceName}.${camel(
157158
method.name.value,
158159
)}(${paramsCallsite}));`;
159-
yield ` if (res.errors.length) {`;
160+
yield ` if (${isError()}(res)) {`;
160161
yield ` const handled: ${QueryError()}<${type(
161162
'Error',
162163
)}[]> = { kind: 'handled', payload: res.errors };`;
@@ -207,7 +208,7 @@ export class HookFile extends ModuleBuilder {
207208
})},`;
208209
yield ` queryFn: async ({ pageParam }: ${PageParam()}) => {`;
209210
yield ` const res = await ${guard()}(${methodExpression}(${paramsCallsite}));`;
210-
yield ` if (res.errors.length) {`;
211+
yield ` if (${isError()}(res)) {`;
211212
yield ` const handled: ${QueryError()}<${type(
212213
'Error',
213214
)}[]> = { kind: 'handled', payload: res.errors };`;
@@ -364,6 +365,7 @@ export class HookFile extends ModuleBuilder {
364365
const queryOptions = this.buildQueryOptions(method);
365366
const QueryError = () => this.runtime.type('QueryError');
366367
const assert = () => this.runtime.fn('assert');
368+
const isError = () => this.runtime.fn('isError');
367369
const type = (t: string) => this.types.type(t);
368370

369371
const serviceName = camel(`${this.int.name.value}_service`);
@@ -392,7 +394,7 @@ export class HookFile extends ModuleBuilder {
392394
yield ` const res = await ${guard()}(${serviceName}.${camel(
393395
method.name.value,
394396
)}(${paramsCallsite}));`;
395-
yield ` if (res.errors.length) {`;
397+
yield ` if (${isError()}(res)) {`;
396398
yield ` const handled: ${QueryError()}<${type(
397399
'Error',
398400
)}[]> = { kind: 'handled', payload: res.errors };`;

src/runtime-file.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ export class RuntimeFile extends ModuleBuilder {
118118
) as any;
119119
120120
return Object.keys(result).length ? result : undefined;
121-
}`;
121+
}
122+
123+
export function isError(result: { errors: { status?: number | string }[]; data?: any; }): boolean {
124+
return !!result.errors.length &&
125+
(Array.isArray(result.data)
126+
? result.data.length === 0
127+
: result.data === undefined);
128+
}
129+
`;
122130
}
123131
}

0 commit comments

Comments
 (0)