Skip to content

Commit

Permalink
feat: add support for URL objects
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Aug 1, 2024
1 parent f37d399 commit 6d0aff1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/astro/src/actions/runtime/virtual/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ export function serializeActionResult(res: SafeResult<any, any>): SerializedActi
type: 'data',
status: 200,
contentType: 'application/json+devalue',
body: devalueStringify(res.data),
body: devalueStringify(res.data, {
// Add support for URL objects
URL: (value) => value instanceof URL && value.href,
}),
};
}

Expand All @@ -236,5 +239,10 @@ export function deserializeActionResult(res: SerializedActionResult): SafeResult
if (res.type === 'empty') {
return { data: undefined, error: undefined };
}
return { data: devalueParse(res.body), error: undefined };
return {
data: devalueParse(res.body, {
URL: (href) => new URL(href),
}),
error: undefined,
};
}
6 changes: 4 additions & 2 deletions packages/astro/test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe('Astro Actions', () => {
assert.equal(value, false);
});

it('Supports complex values like Date and Set', async () => {
it('Supports complex values: Date, Set, URL', async () => {
const req = new Request('http://example.com/_actions/complexValues', {
method: 'POST',
headers: {
Expand All @@ -298,7 +298,9 @@ describe('Astro Actions', () => {
assert.equal(res.status, 200);
assert.equal(res.headers.get('Content-Type'), 'application/json+devalue');

const value = devalue.parse(await res.text());
const value = devalue.parse(await res.text(), {
URL: (href) => new URL(href),
});
assert.ok(value.date instanceof Date);
assert.ok(value.set instanceof Set);
});
Expand Down
1 change: 1 addition & 0 deletions packages/astro/test/fixtures/actions/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const server = {
return {
date: new Date(),
set: new Set(),
url: new URL('https://example.com'),
}
}
})
Expand Down

0 comments on commit 6d0aff1

Please sign in to comment.