Skip to content

Commit

Permalink
Fix serializeRequestBody
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Aug 29, 2024
1 parent 0a74727 commit 2a7afb6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/rest-client-utils/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,20 @@ function isRelArray(object: unknown): object is Rel[] {
return Array.isArray(object) && object.every(isRel);
}

export function serializeRequestBody<T>(body: unknown, options: Options): T {
export function serializeRequestBody<T extends { data: unknown }>(
body: unknown,
options: Options,
): T {
if (typeof body !== 'object' || !body) {
throw new Error('Invalid body!');
}

if (Array.isArray(body)) {
return {
data: body.map((entity) => serializeRequestBody<T>(entity, options).data),
} as unknown as T;
}

const { id, type, meta, ...otherProperties } = body as any;

const attributes: Record<string, unknown> = {};
Expand Down

0 comments on commit 2a7afb6

Please sign in to comment.