diff --git a/src/operations/snapshots/sponsored-products-snapshot-operation.ts b/src/operations/snapshots/sponsored-products-snapshot-operation.ts index 4fef78a05..821993661 100644 --- a/src/operations/snapshots/sponsored-products-snapshot-operation.ts +++ b/src/operations/snapshots/sponsored-products-snapshot-operation.ts @@ -5,7 +5,7 @@ import { SnapshotResponse, RequestSnapshotParams, SnapshotId, - RecordTypeRequest, + SponsoredProductsRecordType, SuccessSnapshotResponse, SnapshotResultType, } from './types' @@ -29,13 +29,13 @@ export class SponsoredProductsSnapshotOperation extends Operation { /** * Request a file-based snapshot of all entities of the specified type in the account satisfying the filtering criteria * - * @param {RecordTypeRequest} recordType + * @param {SponsoredProductsRecordType} recordType * @param {RequestSnapshotParams} params * @returns * @memberof SponsoredProductsSnapshotOperation */ @Decode(SnapshotResponse) - public requestSnapshot(recordType: RecordTypeRequest, params: RequestSnapshotParams) { + public requestSnapshot(recordType: SponsoredProductsRecordType, params: RequestSnapshotParams) { return this.client.post(`${this.resource}${recordType}/snapshot`, params) } diff --git a/src/operations/snapshots/types.ts b/src/operations/snapshots/types.ts index fc767781b..26333e4b5 100644 --- a/src/operations/snapshots/types.ts +++ b/src/operations/snapshots/types.ts @@ -1,5 +1,4 @@ import * as t from 'io-ts' -import { createEnumType } from '../commons/types' import { DateFromNumber } from 'io-ts-types/lib/DateFromNumber' import { Campaign } from '../campaigns/types' import { AdGroup } from '../ad-groups/types' @@ -10,54 +9,39 @@ import { TargetingClause, NegativeTargetingClause } from '../product-targeting/t export const SnapshotId = t.string export type SnapshotId = t.TypeOf -export enum RecordTypeRequestEnum { - CAMPAIGNS = 'campaigns', - AD_GROUPS = 'adGroups', - PRODUCT_ADS = 'productAds', - KEYWORDS = 'keywords', - NEGATIVE_KEYWORDS = 'negativeKeywords', - CAMPAIGN_NEGATIVE_KEYWORDS = 'campaignNegativeKeywords', - TARGETS = 'targets', - NEGATIVE_TARGETS = 'negativeTargets', -} -export const RecordTypeRequest = createEnumType(RecordTypeRequestEnum) -export type RecordTypeRequest = t.TypeOf - -export enum SponsoredBrandsRecordTypeEnum { - CAMPAIGNS = 'campaigns', - KEYWORDS = 'keywords', -} -export const SponsoredBrandsRecordType = createEnumType( - SponsoredBrandsRecordTypeEnum, -) +export const SponsoredProductsRecordType = t.union([ + t.literal('campaigns'), + t.literal('adGroups'), + t.literal('productAds'), + t.literal('keywords'), + t.literal('negativeKeywords'), + t.literal('campaignNegativeKeywords'), + t.literal('targets'), + t.literal('negativeTargets'), +]) +export type SponsoredProductsRecordType = t.TypeOf + +export const SponsoredBrandsRecordType = t.union([t.literal('campaigns'), t.literal('keywords')]) export type SponsoredBrandsRecordType = t.TypeOf -export enum RecordTypeEnum { - CAMPAIGN = 'campaign', - AD_GROUP = 'adGroup', - PRODUCT_AD = 'productAd', - KEYWORD = 'keyword', - NEGATIVE_KEYWORD = 'negativeKeyword', - CAMPAIGN_NEGATIVE_KEYWORD = 'campaignNegativeKeyword', - TARGET = 'target', - NEGATIVE_TARGET = 'negativeTarget', -} -export const RecordType = createEnumType(RecordTypeEnum) -export type RecordType = t.TypeOf - -export enum SnapshotStatusEnum { - IN_PROGRESS = 'IN_PROGRESS', - SUCCESS = 'SUCCESS', - FAILURE = 'FAILURE', -} -export const SnapshotStatusType = createEnumType(SnapshotStatusEnum) - -export enum SnapshotStateEnum { - ENABLED = 'enabled', - PAUSED = 'paused', - ARCHIVED = 'archived', -} -export const SnapshotStateType = createEnumType(SnapshotStateEnum) +export const RecordTypeResponse = t.union([ + t.literal('campaign'), + t.literal('adGroup'), + t.literal('productAd'), + t.literal('keyword'), + t.literal('negativeKeyword'), + t.literal('campaignNegativeKeyword'), + t.literal('target'), + t.literal('negativeTarget'), +]) +export type RecordTypeResponse = t.TypeOf + +export const SnapshotState = t.union([ + t.literal('enabled'), + t.literal('paused'), + t.literal('archived'), +]) +export type SnapshotState = t.TypeOf const BaseSnapshotResponse = t.strict({ /** @@ -72,7 +56,7 @@ export const SuccessSnapshotResponse = t.intersection([ /** * The status of the generation of the snapshot. */ - status: t.literal(SnapshotStatusEnum.SUCCESS), + status: t.literal('SUCCESS'), }), t.partial({ /** @@ -104,7 +88,7 @@ export const InProgressSnapshotResponse = t.intersection([ /** * The status of the generation of the snapshot. */ - status: t.literal(SnapshotStatusEnum.IN_PROGRESS), + status: t.literal('IN_PROGRESS'), }), t.partial({ /** @@ -114,8 +98,9 @@ export const InProgressSnapshotResponse = t.intersection([ /** * The record type of the report. + * TODO: Need check again on Production API. Sandbox API returns singular. Not same in the docs. */ - recordType: RecordType, + recordType: RecordTypeResponse, }), ]) export type InProgressSnapshotResponse = t.TypeOf @@ -126,7 +111,7 @@ export const FailureSnapshotResponse = t.intersection([ /** * The status of the generation of the snapshot. */ - status: t.literal(SnapshotStatusEnum.FAILURE), + status: t.literal('FAILURE'), /** * Description of the status. @@ -152,7 +137,7 @@ export const RequestSnapshotParams = t.partial({ * Must be one of: `enabled`, `paused`, `archived`. * Default behavior is to include enabled and paused. */ - stateFilter: SnapshotStateType, + stateFilter: SnapshotState, }) export type RequestSnapshotParams = t.TypeOf diff --git a/test/index.test.ts b/test/index.test.ts index 7fa826b72..59d6893ab 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -186,13 +186,10 @@ describe('index', () => { describe('snapshot', () => { it('should export all enums', () => { - expect(index.RecordTypeEnum).toEqual(snapshotEnum.RecordTypeEnum) - expect(index.RecordTypeRequestEnum).toEqual(snapshotEnum.RecordTypeRequestEnum) - expect(index.SnapshotStateEnum).toEqual(snapshotEnum.SnapshotStateEnum) - expect(index.SnapshotStatusEnum).toEqual(snapshotEnum.SnapshotStatusEnum) - expect(index.SponsoredBrandsRecordTypeEnum).toEqual( - snapshotEnum.SponsoredBrandsRecordTypeEnum, - ) + expect(index.SponsoredProductsRecordType).toEqual(snapshotEnum.SponsoredProductsRecordType) + expect(index.SponsoredBrandsRecordType).toEqual(snapshotEnum.SponsoredBrandsRecordType) + expect(index.SnapshotState).toEqual(snapshotEnum.SnapshotState) + expect(index.SponsoredBrandsRecordType).toEqual(snapshotEnum.SponsoredBrandsRecordType) }) }) }) diff --git a/test/operations/snapshots/sponsored-brands-snapshot-operation.test.ts b/test/operations/snapshots/sponsored-brands-snapshot-operation.test.ts index b3e78a059..398122116 100644 --- a/test/operations/snapshots/sponsored-brands-snapshot-operation.test.ts +++ b/test/operations/snapshots/sponsored-brands-snapshot-operation.test.ts @@ -1,12 +1,7 @@ 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 { - SnapshotStateEnum, - SnapshotStatusEnum, - SponsoredBrandsRecordTypeEnum, - SuccessSnapshotResponse, -} from '../../../src/operations/snapshots/types' +import { SuccessSnapshotResponse } from '../../../src/operations/snapshots/types' import { HttpClient, Campaign } from '../../../src' jest.setTimeout(15000) @@ -24,7 +19,7 @@ describe('SponsoredBrandsSnapshotOperation', () => { it(`should return a snapshot uncompressed`, async () => { const param: SuccessSnapshotResponse = { snapshotId: 'amzn1.clicksAPI.v1.p1.5E82F8C3.b1095870-c8b5-47c2-b4ef-0c404c3e4fc9', - status: SnapshotStatusEnum.SUCCESS, + status: 'SUCCESS', statusDetails: 'Snapshot has been successfully generated.', location: 'https://advertising-api-test.amazon.com/v1/snapshots/amzn1.clicksAPI.v1.p1.5E82F8C3.b1095870-c8b5-47c2-b4ef-0c404c3e4fc9/download', @@ -40,26 +35,23 @@ describe('SponsoredBrandsSnapshotOperation', () => { describe('requestSnapshot', () => { it(`should return a snapshot report for all entities of a single record type`, async () => { - const res = await operation.requestSnapshot(SponsoredBrandsRecordTypeEnum.CAMPAIGNS, {}) + const res = await operation.requestSnapshot('campaigns', {}) - expect(res.status).toEqual(SnapshotStatusEnum.IN_PROGRESS) + expect(res.status).toBe('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, + const res = await operation.requestSnapshot('keywords', { + stateFilter: 'archived', }) - expect(res.status).toEqual(SnapshotStatusEnum.IN_PROGRESS) + expect(res.status).toBe('IN_PROGRESS') }) }) describe('getSnapshot', () => { it(`should return a snapshot with a specific snapshot id`, async () => { - const requestSnapshotResponse = await operation.requestSnapshot( - SponsoredBrandsRecordTypeEnum.CAMPAIGNS, - {}, - ) + const requestSnapshotResponse = await operation.requestSnapshot('campaigns', {}) const res = await operation.getSnapshot(requestSnapshotResponse.snapshotId) diff --git a/test/operations/snapshots/sponsored-products-snapshot-operation.test.ts b/test/operations/snapshots/sponsored-products-snapshot-operation.test.ts index 9db64ee78..7a8980744 100644 --- a/test/operations/snapshots/sponsored-products-snapshot-operation.test.ts +++ b/test/operations/snapshots/sponsored-products-snapshot-operation.test.ts @@ -1,12 +1,7 @@ import { OperationProvider } from '../../../src/operations/operation-provider' import { SponsoredProductsSnapshotOperation } from '../../../src/operations/snapshots/sponsored-products-snapshot-operation' import { httpClientFactory } from '../../http-client-factory' -import { - RecordTypeRequestEnum, - SnapshotStateEnum, - SnapshotStatusEnum, - SuccessSnapshotResponse, -} from '../../../src/operations/snapshots/types' +import { SuccessSnapshotResponse } from '../../../src/operations/snapshots/types' import { Keyword, KeywordMatchType, KeywordState } from '../../../src' describe('SponsoredProductsSnapshotOperation', () => { @@ -18,7 +13,7 @@ describe('SponsoredProductsSnapshotOperation', () => { it(`should return a snapshot uncompressed`, async () => { const param: SuccessSnapshotResponse = { snapshotId: 'amzn1.clicksAPI.v1.p1.5E820B56.b56140e7-dae5-4188-b8d6-001bb9845843', - status: SnapshotStatusEnum.SUCCESS, + status: 'SUCCESS', statusDetails: 'Snapshot has been successfully generated.', location: 'https://advertising-api-test.amazon.com/v1/snapshots/amzn1.clicksAPI.v1.p1.5E820B56.b56140e7-dae5-4188-b8d6-001bb9845843/download', @@ -39,32 +34,29 @@ describe('SponsoredProductsSnapshotOperation', () => { describe('requestSnapshot', () => { it(`should return a snapshot report for all entities of a single record type`, async () => { - const res = await operation.requestSnapshot(RecordTypeRequestEnum.CAMPAIGNS, {}) + const res = await operation.requestSnapshot('campaigns', {}) - expect(res.status).toEqual(SnapshotStatusEnum.IN_PROGRESS) + expect(res.status).toEqual('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, + const res = await operation.requestSnapshot('adGroups', { + stateFilter: 'archived', }) - expect(res.status).toEqual(SnapshotStatusEnum.IN_PROGRESS) + expect(res.status).toEqual('IN_PROGRESS') }) }) describe('getSnapshot', () => { it(`should return a snapshot with a specific snapshot id`, async () => { expect.assertions(4) - const requestSnapshotResponse = await operation.requestSnapshot( - RecordTypeRequestEnum.KEYWORDS, - {}, - ) + const requestSnapshotResponse = await operation.requestSnapshot('keywords', {}) const res = await operation.getSnapshot(requestSnapshotResponse.snapshotId) expect(res.snapshotId).toBe(requestSnapshotResponse.snapshotId) - if (res.status == SnapshotStatusEnum.SUCCESS) { + if (res.status == 'SUCCESS') { expect(res).toHaveProperty('location') expect(res).toHaveProperty('fileSize') expect(res).toHaveProperty('statusDetails')