Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/hook-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class HookFile extends ModuleBuilder {
const PageParam = () => this.runtime.type('PageParam');
const QueryError = () => this.runtime.type('QueryError');
const assert = () => this.runtime.fn('assert');
const isError = () => this.runtime.fn('isError');

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

Expand Down Expand Up @@ -156,7 +157,7 @@ export class HookFile extends ModuleBuilder {
yield ` const res = await ${guard()}(${serviceName}.${camel(
method.name.value,
)}(${paramsCallsite}));`;
yield ` if (res.errors.length) {`;
yield ` if (${isError()}(res)) {`;
yield ` const handled: ${QueryError()}<${type(
'Error',
)}[]> = { kind: 'handled', payload: res.errors };`;
Expand Down Expand Up @@ -207,7 +208,7 @@ export class HookFile extends ModuleBuilder {
})},`;
yield ` queryFn: async ({ pageParam }: ${PageParam()}) => {`;
yield ` const res = await ${guard()}(${methodExpression}(${paramsCallsite}));`;
yield ` if (res.errors.length) {`;
yield ` if (${isError()}(res)) {`;
yield ` const handled: ${QueryError()}<${type(
'Error',
)}[]> = { kind: 'handled', payload: res.errors };`;
Expand Down Expand Up @@ -364,6 +365,7 @@ export class HookFile extends ModuleBuilder {
const queryOptions = this.buildQueryOptions(method);
const QueryError = () => this.runtime.type('QueryError');
const assert = () => this.runtime.fn('assert');
const isError = () => this.runtime.fn('isError');
const type = (t: string) => this.types.type(t);

const serviceName = camel(`${this.int.name.value}_service`);
Expand Down Expand Up @@ -392,7 +394,7 @@ export class HookFile extends ModuleBuilder {
yield ` const res = await ${guard()}(${serviceName}.${camel(
method.name.value,
)}(${paramsCallsite}));`;
yield ` if (res.errors.length) {`;
yield ` if (${isError()}(res)) {`;
yield ` const handled: ${QueryError()}<${type(
'Error',
)}[]> = { kind: 'handled', payload: res.errors };`;
Expand Down
10 changes: 9 additions & 1 deletion src/runtime-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ export class RuntimeFile extends ModuleBuilder {
) as any;

return Object.keys(result).length ? result : undefined;
}`;
}

export function isError(result: { errors: { status?: number | string }[]; data?: any; }): boolean {
return !!result.errors.length &&
(Array.isArray(result.data)
? result.data.length === 0
: result.data === undefined);
}
`;
}
}