From 6d0aff182dffa6a3011d90cd2c52b59c7eeace39 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 1 Aug 2024 13:48:15 -0400 Subject: [PATCH] feat: add support for URL objects --- packages/astro/src/actions/runtime/virtual/shared.ts | 12 ++++++++++-- packages/astro/test/actions.test.js | 6 ++++-- .../astro/test/fixtures/actions/src/actions/index.ts | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/astro/src/actions/runtime/virtual/shared.ts b/packages/astro/src/actions/runtime/virtual/shared.ts index 4032e226ad71..fda32f75420a 100644 --- a/packages/astro/src/actions/runtime/virtual/shared.ts +++ b/packages/astro/src/actions/runtime/virtual/shared.ts @@ -225,7 +225,10 @@ export function serializeActionResult(res: SafeResult): 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, + }), }; } @@ -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, + }; } diff --git a/packages/astro/test/actions.test.js b/packages/astro/test/actions.test.js index 77ffa1b5af85..8e5919b3159e 100644 --- a/packages/astro/test/actions.test.js +++ b/packages/astro/test/actions.test.js @@ -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: { @@ -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); }); diff --git a/packages/astro/test/fixtures/actions/src/actions/index.ts b/packages/astro/test/fixtures/actions/src/actions/index.ts index 1c79bd93298c..bc61ade3ab63 100644 --- a/packages/astro/test/fixtures/actions/src/actions/index.ts +++ b/packages/astro/test/fixtures/actions/src/actions/index.ts @@ -79,6 +79,7 @@ export const server = { return { date: new Date(), set: new Set(), + url: new URL('https://example.com'), } } })