Skip to content

Commit

Permalink
Make origin variable decoder returns an object instead of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-ka committed Jun 1, 2023
1 parent ef9e4b5 commit e51c9fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/VariableDecoder/origin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
},
),
);
Expand Down
10 changes: 8 additions & 2 deletions src/VariableDecoder/origin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import * as RE from 'fp-ts/ReaderEither';

import { ask, decodeFailed, validate, VariableDecoder } from './VariableDecoder';

type Origin = Pick<URL, 'protocol' | 'hostname' | 'port'>;

/**
* Decodes a URL origin (scheme + hostname + port).
*/
const origin = (): VariableDecoder<string> =>
const origin = (): VariableDecoder<Origin> =>
pipe(
RE.Do,
RE.bind('value', () => ask()),
Expand All @@ -19,7 +21,11 @@ const origin = (): VariableDecoder<string> =>
),
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 };

0 comments on commit e51c9fc

Please sign in to comment.