From e51c9fc40544d79ac89438f25bf32dfb38555db6 Mon Sep 17 00:00:00 2001 From: Ryota Kameoka Date: Thu, 1 Jun 2023 17:44:33 +0900 Subject: [PATCH] Make `origin` variable decoder returns an object instead of a string --- src/VariableDecoder/origin.test.ts | 6 +++++- src/VariableDecoder/origin.ts | 10 ++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/VariableDecoder/origin.test.ts b/src/VariableDecoder/origin.test.ts index 6dcf339..067782f 100644 --- a/src/VariableDecoder/origin.test.ts +++ b/src/VariableDecoder/origin.test.ts @@ -20,7 +20,11 @@ describe(origin, () => { .map(([url, port]) => `${new URL(url).origin}:${port}`), (str) => { const variable = new Variable('KEY', str); - expect(decoder(variable)).toStrictEqual(E.right(str)); + + const { protocol, hostname, port } = new URL(str); + const expected = { protocol, hostname, port }; + + expect(decoder(variable)).toStrictEqual(E.right(expected)); }, ), ); diff --git a/src/VariableDecoder/origin.ts b/src/VariableDecoder/origin.ts index b78d896..c542a86 100644 --- a/src/VariableDecoder/origin.ts +++ b/src/VariableDecoder/origin.ts @@ -4,10 +4,12 @@ import * as RE from 'fp-ts/ReaderEither'; import { ask, decodeFailed, validate, VariableDecoder } from './VariableDecoder'; +type Origin = Pick; + /** * Decodes a URL origin (scheme + hostname + port). */ -const origin = (): VariableDecoder => +const origin = (): VariableDecoder => pipe( RE.Do, RE.bind('value', () => ask()), @@ -19,7 +21,11 @@ const origin = (): VariableDecoder => ), RE.chain(validate(({ url }) => url.pathname !== '', 'must not have a pathname')), RE.chain(validate(({ value, url }) => url.origin === value, 'must be a valid URL origin')), - RE.map(({ value }) => value), + RE.map(({ url: { protocol, hostname, port } }) => ({ + protocol, + hostname, + port, + })), ); export { origin };