Skip to content

Commit

Permalink
feat: change snapshot module to use union string
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyentoanit committed Apr 20, 2020
1 parent dafbec1 commit fded09c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SnapshotResponse,
RequestSnapshotParams,
SnapshotId,
RecordTypeRequest,
SponsoredProductsRecordType,
SuccessSnapshotResponse,
SnapshotResultType,
} from './types'
Expand All @@ -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<SnapshotResponse>(`${this.resource}${recordType}/snapshot`, params)
}

Expand Down
89 changes: 37 additions & 52 deletions src/operations/snapshots/types.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -10,54 +9,39 @@ import { TargetingClause, NegativeTargetingClause } from '../product-targeting/t
export const SnapshotId = t.string
export type SnapshotId = t.TypeOf<typeof SnapshotId>

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>(RecordTypeRequestEnum)
export type RecordTypeRequest = t.TypeOf<typeof RecordTypeRequest>

export enum SponsoredBrandsRecordTypeEnum {
CAMPAIGNS = 'campaigns',
KEYWORDS = 'keywords',
}
export const SponsoredBrandsRecordType = createEnumType<SponsoredBrandsRecordTypeEnum>(
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<typeof SponsoredProductsRecordType>

export const SponsoredBrandsRecordType = t.union([t.literal('campaigns'), t.literal('keywords')])
export type SponsoredBrandsRecordType = t.TypeOf<typeof SponsoredBrandsRecordType>

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>(RecordTypeEnum)
export type RecordType = t.TypeOf<typeof RecordType>

export enum SnapshotStatusEnum {
IN_PROGRESS = 'IN_PROGRESS',
SUCCESS = 'SUCCESS',
FAILURE = 'FAILURE',
}
export const SnapshotStatusType = createEnumType<SnapshotStatusEnum>(SnapshotStatusEnum)

export enum SnapshotStateEnum {
ENABLED = 'enabled',
PAUSED = 'paused',
ARCHIVED = 'archived',
}
export const SnapshotStateType = createEnumType<SnapshotStateEnum>(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<typeof RecordTypeResponse>

export const SnapshotState = t.union([
t.literal('enabled'),
t.literal('paused'),
t.literal('archived'),
])
export type SnapshotState = t.TypeOf<typeof SnapshotState>

const BaseSnapshotResponse = t.strict({
/**
Expand All @@ -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({
/**
Expand Down Expand Up @@ -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({
/**
Expand All @@ -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<typeof InProgressSnapshotResponse>
Expand All @@ -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.
Expand All @@ -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<typeof RequestSnapshotParams>

Expand Down
11 changes: 4 additions & 7 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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',
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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',
Expand All @@ -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')
Expand Down

0 comments on commit fded09c

Please sign in to comment.