Skip to content

Commit

Permalink
feat: make discriminated union type for SnapshotResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyentoanit committed Mar 30, 2020
1 parent 3920c8c commit 059d57c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
65 changes: 47 additions & 18 deletions src/operations/snapshots/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,26 @@ export enum SnapshotStateEnum {
}
export const SnapshotStateType = createEnumType<SnapshotStateEnum>(SnapshotStateEnum)

export const SnapshotResponse = t.intersection([
t.strict({
/**
* The ID of the snapshot that was requested.
*/
snapshotId: SnapshotId,
const BaseSnapshotResponse = t.strict({
/**
* The ID of the snapshot that was requested.
*/
snapshotId: SnapshotId,

/**
* The status of the generation of the snapshot.
*/
status: SnapshotStatusType,
}),
t.partial({
/**
* The status of the generation of the snapshot.
*/
status: SnapshotStatusType,
})

export const SuccessSnapshotResponse = t.intersection([
BaseSnapshotResponse,
t.strict({
/**
* The record type of the report.
*/
recordType: RecordType,

/**
* Description of the status.
*/
statusDetails: t.string,

/**
* The URI for the snapshot. It's only available if status is SUCCESS.
*/
Expand All @@ -93,7 +90,39 @@ export const SnapshotResponse = t.intersection([
expiration: DateFromNumber,
}),
])
export type SnapshotResponse = t.TypeOf<typeof SnapshotResponse>
export type SuccessSnapshotResponse = t.TypeOf<typeof SuccessSnapshotResponse>

export const InProgressSnapshotResponse = t.intersection([
BaseSnapshotResponse,
t.strict({
/**
* The record type of the report.
*/
recordType: RecordType,
}),
])
export type InProgressSnapshotResponse = t.TypeOf<typeof InProgressSnapshotResponse>

export const FailureSnapshotResponse = t.intersection([
BaseSnapshotResponse,
t.strict({
/**
* Description of the status.
*/
statusDetails: t.string,
}),
])
export type FailureSnapshotResponse = t.TypeOf<typeof FailureSnapshotResponse>

export const SnapshotResponse = t.union([
SuccessSnapshotResponse,
InProgressSnapshotResponse,
FailureSnapshotResponse,
])
export type SnapshotResponse =
| SuccessSnapshotResponse
| InProgressSnapshotResponse
| FailureSnapshotResponse

export const RequestSnapshotParams = t.partial({
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { OperationProvider } from '../../../src/operations/operation-provider'
import { SponsoredBrandsSnapshotOperation } from '../../../src/operations/snapshots/sponsored-brands-snapshot-operation'
import { SANDBOX_URI, auth } from '../../http-client-factory'
import {
RecordTypeEnum,
SnapshotStateEnum,
SnapshotStatusEnum,
SponsoredBrandsRecordTypeEnum,
Expand All @@ -20,15 +19,17 @@ describe('SponsoredBrandsSnapshotOperation', () => {
it(`should return a snapshot report for all entities of a single record type`, async () => {
const res = await operation.requestSnapshot(SponsoredBrandsRecordTypeEnum.CAMPAIGNS, {})

expect(res.recordType).toBe(RecordTypeEnum.CAMPAIGN)
expect(res).toHaveProperty('recordType')
expect(res.status).toEqual(SnapshotStatusEnum.IN_PROGRESS)
})

it(`should return a snapshot report for all entities of a single record type with additional attributes satisfying optional criteria`, async () => {
const res = await operation.requestSnapshot(SponsoredBrandsRecordTypeEnum.KEYWORDS, {
stateFilter: SnapshotStateEnum.ARCHIVED,
})

expect(res.recordType).toBe(RecordTypeEnum.KEYWORD)
expect(res).toHaveProperty('recordType')
expect(res.status).toEqual(SnapshotStatusEnum.IN_PROGRESS)
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { OperationProvider } from '../../../src/operations/operation-provider'
import { SponsoredProductsSnapshotOperation } from '../../../src/operations/snapshots/sponsored-products-snapshot-operation'
import { httpClientFactory } from '../../http-client-factory'
import {
RecordTypeEnum,
RecordTypeRequestEnum,
SnapshotStateEnum,
SnapshotStatusEnum,
Expand All @@ -18,15 +17,17 @@ describe('SponsoredProductsSnapshotOperation', () => {
it(`should return a snapshot report for all entities of a single record type`, async () => {
const res = await operation.requestSnapshot(RecordTypeRequestEnum.CAMPAIGNS, {})

expect(res.recordType).toBe(RecordTypeEnum.CAMPAIGN)
expect(res).toHaveProperty('recordType')
expect(res.status).toEqual(SnapshotStatusEnum.IN_PROGRESS)
})

it(`should return a snapshot report for all entities of a single record type with additional attributes satisfying optional criteria`, async () => {
const res = await operation.requestSnapshot(RecordTypeRequestEnum.AD_GROUPS, {
stateFilter: SnapshotStateEnum.ARCHIVED,
})

expect(res.recordType).toBe(RecordTypeEnum.AD_GROUP)
expect(res).toHaveProperty('recordType')
expect(res.status).toEqual(SnapshotStatusEnum.IN_PROGRESS)
})
})

Expand Down

1 comment on commit 059d57c

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.