From 059d57c9be995808f8c68dce901b94c0a7e4fbad Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Sun, 29 Mar 2020 22:34:07 +0700 Subject: [PATCH] feat: make discriminated union type for SnapshotResponse --- src/operations/snapshots/types.ts | 65 ++++++++++++++----- ...ponsored-brands-snapshot-operation.test.ts | 7 +- ...nsored-products-snapshot-operation.test.ts | 7 +- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/operations/snapshots/types.ts b/src/operations/snapshots/types.ts index 062de20fd..89d9afc60 100644 --- a/src/operations/snapshots/types.ts +++ b/src/operations/snapshots/types.ts @@ -54,29 +54,26 @@ export enum SnapshotStateEnum { } export const SnapshotStateType = createEnumType(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. */ @@ -93,7 +90,39 @@ export const SnapshotResponse = t.intersection([ expiration: DateFromNumber, }), ]) -export type SnapshotResponse = t.TypeOf +export type SuccessSnapshotResponse = t.TypeOf + +export const InProgressSnapshotResponse = t.intersection([ + BaseSnapshotResponse, + t.strict({ + /** + * The record type of the report. + */ + recordType: RecordType, + }), +]) +export type InProgressSnapshotResponse = t.TypeOf + +export const FailureSnapshotResponse = t.intersection([ + BaseSnapshotResponse, + t.strict({ + /** + * Description of the status. + */ + statusDetails: t.string, + }), +]) +export type FailureSnapshotResponse = t.TypeOf + +export const SnapshotResponse = t.union([ + SuccessSnapshotResponse, + InProgressSnapshotResponse, + FailureSnapshotResponse, +]) +export type SnapshotResponse = + | SuccessSnapshotResponse + | InProgressSnapshotResponse + | FailureSnapshotResponse export const RequestSnapshotParams = t.partial({ /** diff --git a/test/operations/snapshots/sponsored-brands-snapshot-operation.test.ts b/test/operations/snapshots/sponsored-brands-snapshot-operation.test.ts index 1737c0124..f67c69a27 100644 --- a/test/operations/snapshots/sponsored-brands-snapshot-operation.test.ts +++ b/test/operations/snapshots/sponsored-brands-snapshot-operation.test.ts @@ -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, @@ -20,7 +19,8 @@ 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 () => { @@ -28,7 +28,8 @@ describe('SponsoredBrandsSnapshotOperation', () => { stateFilter: SnapshotStateEnum.ARCHIVED, }) - expect(res.recordType).toBe(RecordTypeEnum.KEYWORD) + expect(res).toHaveProperty('recordType') + expect(res.status).toEqual(SnapshotStatusEnum.IN_PROGRESS) }) }) diff --git a/test/operations/snapshots/sponsored-products-snapshot-operation.test.ts b/test/operations/snapshots/sponsored-products-snapshot-operation.test.ts index 468552a4f..2c0c5ffd5 100644 --- a/test/operations/snapshots/sponsored-products-snapshot-operation.test.ts +++ b/test/operations/snapshots/sponsored-products-snapshot-operation.test.ts @@ -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, @@ -18,7 +17,8 @@ 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 () => { @@ -26,7 +26,8 @@ describe('SponsoredProductsSnapshotOperation', () => { stateFilter: SnapshotStateEnum.ARCHIVED, }) - expect(res.recordType).toBe(RecordTypeEnum.AD_GROUP) + expect(res).toHaveProperty('recordType') + expect(res.status).toEqual(SnapshotStatusEnum.IN_PROGRESS) }) })