Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serialization): added the url serializer #262

Merged
merged 2 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/serialization/src/SerializerBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ObjectSerializer from './serializers/ObjectSerializer.js';
import PrimitiveSerializer from './serializers/PrimitiveSerializer.js';
import SetSerializer from './serializers/SetSerializer.js';
import TypedArraySerializer from './serializers/TypedArraySerializer.js';
import UrlSerializer from './serializers/UrlSerializer.js';
import DefaultClassLoader from './DefaultClassLoader.js';

const defaultClassLoader = new DefaultClassLoader();
Expand All @@ -23,6 +24,7 @@ export default class SerializerBuilder
serializer.addSerializer(new ObjectSerializer());
serializer.addSerializer(new ClassSerializer(loader));
serializer.addSerializer(new ErrorSerializer());
serializer.addSerializer(new UrlSerializer());
serializer.addSerializer(new DateSerializer());
serializer.addSerializer(new SetSerializer());
serializer.addSerializer(new MapSerializer());
Expand Down
39 changes: 39 additions & 0 deletions packages/serialization/src/serializers/UrlSerializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

import ValueSerializer from '../ValueSerializer.js';
import SerializedUrl from '../types/serialized/SerializedUrl.js';
import InvalidUrlString from './errors/InvalidUrlString.js';

export default class UrlSerializer extends ValueSerializer
{
canSerialize(value: unknown): boolean
{
return value instanceof URL;
}

canDeserialize(value: unknown): boolean
{
const url = value as SerializedUrl;

return url instanceof Object
&& url.serialized === true
&& url.name === 'Url'
&& typeof url.value === 'string';
}

async serialize(url: URL): Promise<SerializedUrl>
{
return { serialized: true, name: 'Url', value: url.toString() };
}

async deserialize(object: SerializedUrl): Promise<URL>
{
try
{
return new URL(object.value);
}
catch (error)
{
throw new InvalidUrlString(object.value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export default class InvalidUrlString extends Error
{
constructor(urlString: string)
{
super(`Invalid url string: '${urlString}'`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
super(`Invalid url string: '${urlString}'`);
super(`The url string '${urlString}' is invalid`);

Same format as the InvalidClass error

}
}
9 changes: 9 additions & 0 deletions packages/serialization/src/types/serialized/SerializedUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import Serialized from '../Serialized.js';

type SerializedUrl = Serialized &
{
value: string
};

export default SerializedUrl;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

const validURL = new URL('https://example.com?foo=bar&baz=qux#quux');

const serializedValidUrl = { serialized: true, name: 'Url', value: 'https://example.com/?foo=bar&baz=qux#quux' };

const nonObject = 42;
const nonUrl = new Map();
const notSerialized = { name: 'Url', bytes: [] };
const invalidName = { serialized: true, name: 'Map', value: '2021-01-01T00:00:00.000Z' };
const invalidUrlValue = { serialized: true, name: 'Date', value: true };
const invalidUrlString = { serialized: true, name: 'Date', value: 'example' };

export
{
validURL,
serializedValidUrl,
nonObject, nonUrl, notSerialized, invalidName, invalidUrlValue, invalidUrlString
};
86 changes: 86 additions & 0 deletions packages/serialization/test/serializers/UrlSerializer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

import { describe, expect, it } from 'vitest';

import UrlSerializer from '../../src/serializers/UrlSerializer';
import InvalidUrlString from '../../src/serializers/errors/InvalidUrlString';

import
{
validURL,
serializedValidUrl,
nonObject, nonUrl, notSerialized, invalidName, invalidUrlValue, invalidUrlString
} from '../_fixtures/serializers/UrlSerializer.fixture';

const serializer = new UrlSerializer();

describe('serializers/UrlSerializer', () =>
{
describe('.canSerialize(value)', () =>
{
it('should tell it can serialize an url', () =>
{
const supportsDate = serializer.canSerialize(validURL);

expect(supportsDate).toBeTruthy();
});

it('should tell it cannot serialize others', () =>
{
const supportsNonObject = serializer.canSerialize(nonObject);
const supportsNonDate = serializer.canSerialize(nonUrl);

expect(supportsNonObject).toBeFalsy();
expect(supportsNonDate).toBeFalsy();
});
});

describe('.canDeserialize(value)', () =>
{
it('should tell it can deserialize an url', () =>
{
const supportsDate = serializer.canDeserialize(serializedValidUrl);

expect(supportsDate).toBeTruthy();
});

it('should tell it cannot deserialize others', () =>
{
const supportsNonObject = serializer.canDeserialize(nonObject);
const supportsNotSerialized = serializer.canDeserialize(notSerialized);
const supportsInvalidName = serializer.canDeserialize(invalidName);
const supportsInvalidDateValue = serializer.canDeserialize(invalidUrlValue);

expect(supportsNonObject).toBeFalsy();
expect(supportsNotSerialized).toBeFalsy();
expect(supportsInvalidName).toBeFalsy();
expect(supportsInvalidDateValue).toBeFalsy();
});
});

describe('.serialize(url)', () =>
{
it('should serialize a url', async () =>
{
const resultFixedDate = await serializer.serialize(validURL);

expect(resultFixedDate).toStrictEqual(serializedValidUrl);
});
});

describe('.deserialize(object)', () =>
{
it('should deserialize an url', async () =>
{
const resultFixedDate = await serializer.deserialize(serializedValidUrl);

expect(resultFixedDate).toStrictEqual(validURL);
});

it('should not deserialize an url with an invalid url string', async () =>
{
const deserialize = async () => await serializer.deserialize(invalidUrlString);

expect(deserialize).rejects.toStrictEqual(new InvalidUrlString('example'));
});
});
});