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

Refactor (add more typing) #13

Merged
merged 6 commits into from
May 1, 2022
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
Prev Previous commit
Next Next commit
chore: increase test coverage
  • Loading branch information
andersondanilo committed May 1, 2022
commit b83ef9251dc47e6ce99ac830cdce754e9a22e568
3 changes: 2 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Transformer } from './transformer'
import { Options, DocumentObject, JsonApiFractalError } from './types'
import { Options, DocumentObject } from './types'
import { JsonApiFractalError } from './errors'

export type Context<TEntity, TExtraProperties = unknown> = {
input: TEntity | undefined
Expand Down
6 changes: 6 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class JsonApiFractalError extends Error {
constructor(message: string) {
super(message)
this.name = 'JsonApiFractalError'
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { Context } from './context'
export { deserialize } from './deserializer'
export { serialize, transform } from './serializer'
export { whitelist } from './utils'
export { JsonApiFractalError } from './errors'
9 changes: 5 additions & 4 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
ResourceObject,
RelationshipObject,
NewResourceObject,
JsonApiFractalError,
ResourceIdentifierObject,
SerializeOptions,
} from './types'
import { whitelist, changeCase } from './utils'
import { JsonApiFractalError } from './errors'

type IncludedRecord = Record<string, Record<string, ResourceObject>>

Expand All @@ -21,10 +22,10 @@ export function transform<TEntity, TExtraOptions = unknown>(): ContextBuilder<TE
export function serialize<TEntity, TExtraOptions = unknown>(
data: TEntity,
type: string,
options: Options<TExtraOptions> & { relationships?: string[] },
options?: SerializeOptions<TExtraOptions>,
): DocumentObject {
if (!options) {
options = {} as Options<TExtraOptions>
options = {} as SerializeOptions<TExtraOptions>
}

return transform()
Expand Down Expand Up @@ -79,7 +80,7 @@ function serializeEntity<TEntity, TExtraOptions>(

for (const relation of Object.keys(transformer.relationships)) {
const context: Context<unknown, TExtraOptions> = {
...transformer.relationships[relation](entity),
...transformer.relationships[relation](entity, options),
options,
}

Expand Down
1 change: 1 addition & 0 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type TransformerRelationships<TEntity, TExtraOptions> = Record<

export type RelationshipTransformerInfoFunction<TEntity, TExtraOptions> = (
entity: TEntity,
options: Options<TExtraOptions>,
) => RelationshipTransformerInfo<TExtraOptions>

export type RelationshipTransformerInfo<TExtraOptions, T = unknown> = {
Expand Down
7 changes: 1 addition & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,4 @@ export type Options<TExtraOptions = void> = {
extra?: TExtraOptions
}

export class JsonApiFractalError extends Error {
constructor(message: string) {
super(message)
this.name = 'JsonApiFractalError'
}
}
export type SerializeOptions<TExtraOptions = void> = Options<TExtraOptions> & { relationships?: string[] }
18 changes: 18 additions & 0 deletions tests/context.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ContextBuilder } from '../src/context'
import { JsonApiFractalError } from '../src/errors'

describe('ContextBuilder', () => {
describe('toContext', () => {
it('should throw exception when the transformer was not provided', () => {
expect(() => {
new ContextBuilder(
() =>
({
name: 'test',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any),
).toContext()
}).toThrow(JsonApiFractalError)
})
})
})
11 changes: 11 additions & 0 deletions tests/errors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { JsonApiFractalError } from '../src/errors'

describe('JsonApiFractalError', () => {
describe('constructor', () => {
it('should set the name', () => {
const error = new JsonApiFractalError('my message')
expect(error.name).toEqual('JsonApiFractalError')
expect(error.message).toEqual('my message')
})
})
})
112 changes: 112 additions & 0 deletions tests/serialize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { serialize } from '../src'
import { CaseType, SerializeOptions } from '../src/types'

describe('serialize', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let validEntity: any
let validOptions: SerializeOptions

beforeEach(() => {
validEntity = {
firstName: 'Joe',
lastName: 'Doe',
address: {
id: 'address-1',
},
images: [
{ id: 'image-1', name: 'myimage1', width: 100 },
{ id: 'image-2', name: 'myimage2', width: 100 },
],
}

validOptions = {
relationships: ['address', 'images'],
changeCase: CaseType.kebabCase,
}
})

it('should do simple transformation', () => {
const serialized = serialize(validEntity, 'users', validOptions)

expect(serialized).toEqual({
data: {
type: 'users',
attributes: {
'first-name': 'Joe',
'last-name': 'Doe',
},
relationships: {
address: {
data: {
type: 'address',
id: 'address-1',
},
},
images: {
data: [
{ type: 'images', id: 'image-1' },
{ type: 'images', id: 'image-2' },
],
},
},
},
})
})

it('should accept undefined options', () => {
const serialized = serialize(validEntity, 'users')

expect(serialized).toEqual(
expect.objectContaining({
data: expect.objectContaining({
attributes: expect.objectContaining({
firstName: 'Joe',
}),
}),
}),
)
})

it('should accept empty entity', () => {
const serialized = serialize(undefined, 'users')

expect(serialized).toEqual({
// eslint-disable-next-line unicorn/no-null
data: null,
})
})

it('should accept entity array', () => {
const serialized = serialize([validEntity], 'users')

expect(serialized).toEqual(
expect.objectContaining({
data: [
expect.objectContaining({
attributes: expect.objectContaining({
firstName: 'Joe',
}),
}),
],
}),
)
})

it('should accept fields filter', () => {
const serialized = serialize({ id: 5, ...validEntity }, 'users', {
fields: {
users: ['firstName'],
},
})

expect(serialized).toEqual({
data: {
id: 5,
type: 'users',
attributes: {
firstName: 'Joe',
},
},
})
})
})
44 changes: 0 additions & 44 deletions tests/simple-transform.spec.ts

This file was deleted.

File renamed without changes.