Skip to content

Commit

Permalink
feat: change bidding types to use union types
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyentoanit committed Apr 28, 2020
1 parent 8ee89d6 commit 3e9a656
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 39 deletions.
89 changes: 60 additions & 29 deletions src/operations/bidding/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ export const KeywordBidRecommendationsMatchType = t.union([
])
export type KeywordBidRecommendationsMatchType = t.TypeOf<typeof KeywordBidRecommendationsMatchType>

/**
* The resulting status code for retrieving the bid.
*/
export const BidRecommendationsResponseCode = t.union([
t.literal('SUCCESS'),
t.literal('INVALID_ARGUMENT'),
t.literal('NOT_FOUND'),
t.literal('INTERNAL_ERROR'),
t.literal('SERVER_IS_BUSY'),
t.literal('UNAUTHORIZED'),
])
export type BidRecommendationsResponseCode = t.TypeOf<typeof BidRecommendationsResponseCode>

export const BidRecommendationsKeyword = t.strict({
/**
* The keyword text.
Expand Down Expand Up @@ -89,26 +76,33 @@ export const KeywordBidRecommendationsData = t.strict({
})
export type KeywordBidRecommendationsData = t.TypeOf<typeof KeywordBidRecommendationsData>

const BidRecommendationSuccesResponse = t.intersection([
t.strict({
/**
* The resulting status code for retrieving the bid.
*/
code: t.literal('SUCCESS'),

suggestedBid: SuggestedBid,
}),

BidRecommendationsKeyword,
])

const BidRecommendationNotFound = t.strict({
/**
* The response code
*/
code: t.literal('NOT_FOUND'),
})

export const BidRecommendationsResponse = t.strict({
/**
* The ID of the ad group that a bid was requested for.
*/
adGroupId: AdGroupId,

recommendations: t.array(
t.intersection([
t.strict({
/**
* The resulting status code for retrieving the bid.
*/
code: BidRecommendationsResponseCode,

suggestedBid: SuggestedBid,
}),

BidRecommendationsKeyword,
]),
),
recommendations: t.array(t.union([BidRecommendationSuccesResponse, BidRecommendationNotFound])),
})
export type BidRecommendationsResponse = t.TypeOf<typeof BidRecommendationsResponse>

Expand Down Expand Up @@ -180,7 +174,7 @@ export const BidRecommendationRequest = t.strict({
})
export type BidRecommendationRequest = t.TypeOf<typeof BidRecommendationRequest>

export const BidRecommendationList = t.strict({
const BidRecommendationForTargetsSuccess = t.strict({
/**
* The suggested bid
*/
Expand All @@ -194,9 +188,46 @@ export const BidRecommendationList = t.strict({
/**
* The response code
*/
code: BidRecommendationsResponseCode,
code: t.literal('SUCCESS'),
})

const BidRecommendationInvalidArgument = t.strict({
/**
* The response code
*/
code: t.literal('INVALID_ARGUMENT'),
})

const BidRecommendationInternalError = t.strict({
/**
* The response code
*/
code: t.literal('INTERNAL_ERROR'),
})

const BidRecommendationServerIsBusy = t.strict({
/**
* The response code
*/
code: t.literal('SERVER_IS_BUSY'),
})

const BidRecommendationUnauthorized = t.strict({
/**
* The response code
*/
code: t.literal('UNAUTHORIZED'),
})

export const BidRecommendationList = t.union([
BidRecommendationForTargetsSuccess,
BidRecommendationInvalidArgument,
BidRecommendationNotFound,
BidRecommendationInternalError,
BidRecommendationServerIsBusy,
BidRecommendationUnauthorized,
])

export const BidRecommendationLists = t.array(BidRecommendationList)

/**
Expand Down
3 changes: 0 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ describe('index', () => {
it('should export all enums', () => {
expect(index.CampaignBiddingAdjustmentsPredicate).toEqual(CampaignBiddingAdjustmentsPredicate)
expect(index.CampaignBiddingStrategy).toEqual(CampaignBiddingStrategy)
expect(index.BidRecommendationsResponseCode).toEqual(
biddingTypes.BidRecommendationsResponseCode,
)
expect(index.BiddingAutoPredicateType).toEqual(biddingTypes.BiddingAutoPredicateType)
expect(index.BiddingKeywordPredicateType).toEqual(biddingTypes.BiddingKeywordPredicateType)
expect(index.BiddingProductPredicateType).toEqual(biddingTypes.BiddingProductPredicateType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { httpClientFactory } from '../../http-client-factory'
import { SponsoredProductsBidRecommendationOperation } from '../../../src/operations/bidding/sponsored-products-bid-recommendation-operation'
import {
KeywordBidRecommendationsData,
BidRecommendationsResponseCode,
KeywordBidRecommendationsMatchType,
BidRecommendationRequest,
} from '../../../src'
Expand Down Expand Up @@ -35,6 +34,8 @@ describe('SponsoredProductsBidRecommendationOperation', () => {

describe('createKeywordBidRecommendations', () => {
it(`should retrieve keyword bid recommendation data for one or more keywords`, async () => {
expect.assertions(3)

const params: KeywordBidRecommendationsData = {
adGroupId: MANUAL_AD_GROUP_ID,
keywords: [
Expand All @@ -56,9 +57,10 @@ describe('SponsoredProductsBidRecommendationOperation', () => {
const [recommendation] = res.recommendations

expect(res.adGroupId).toBe(MANUAL_AD_GROUP_ID)
expect(recommendation.code).toBe<BidRecommendationsResponseCode>('SUCCESS')
expect(recommendation.keyword).toBe('Apple1')
expect(recommendation.matchType).toBe<KeywordBidRecommendationsMatchType>('broad')
if (recommendation.code == 'SUCCESS') {
expect(recommendation.keyword).toBe('Apple1')
expect(recommendation.matchType).toBe<KeywordBidRecommendationsMatchType>('broad')
}
})
})

Expand All @@ -69,6 +71,7 @@ describe('SponsoredProductsBidRecommendationOperation', () => {
*/
describe.skip('getBidRecommendations', () => {
it(`should retrieve a list of bid recommendations for keyword, product or auto targeting expressions by adGroupId`, async () => {
expect.assertions(3)
const EXPRESSION_VALUE = 'Apple'
const EXPRESSION_TYPE = 'queryBroadRelMatches'

Expand All @@ -85,9 +88,10 @@ describe('SponsoredProductsBidRecommendationOperation', () => {
const [recommendation] = res.recommendations

expect(res.adGroupId).toBe(AUTO_AD_GROUP_ID)
expect(recommendation.code).toBe<BidRecommendationsResponseCode>('SUCCESS')
expect(recommendation.expression.value).toBe(EXPRESSION_VALUE)
expect(recommendation.expression.type).toBe(EXPRESSION_TYPE)
if (recommendation.code == 'SUCCESS') {
expect(recommendation.expression.value).toBe(EXPRESSION_VALUE)
expect(recommendation.expression.type).toBe(EXPRESSION_TYPE)
}
})
})
})

1 comment on commit 3e9a656

@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.