Skip to content
This repository was archived by the owner on Jul 13, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"io-ts-promise": "2.0.2",
"io-ts-reporters": "1.2.2",
"io-ts-types": "0.5.16",
"json-bigint": "1.0.0",
"lazy-get-decorator": "2.2.0",
"lodash": "4.17.21",
"ts-error": "1.0.6"
Expand All @@ -51,6 +52,7 @@
"@scaleleap/jest-polly": "1.5.27",
"@scaleleap/semantic-release-config": "1.1.40",
"@types/jest": "27.4.0",
"@types/json-bigint": "1.0.1",
"@types/lodash": "4.14.182",
"@types/node": "16.11.21",
"@typescript-eslint/eslint-plugin": "2.34.0",
Expand Down
7 changes: 4 additions & 3 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ export function Decode(decoder: Decoder): Function {
return {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: function value(...args: any[]) {
return Promise.resolve(originalMethod.apply(this, args)).then((res) =>
tPromise.decode(decoder, res),
)
return Promise.resolve(originalMethod.apply(this, args)).then((res) => {
console.log('res', res)
return tPromise.decode(decoder, res)
})
},
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { axios, Method, AxiosResponse } from './axios'
import HttpStatus from 'http-status-codes'
import jsonBigInt from 'json-bigint'

import { JSON_CONTENT_TYPE } from './constants'
import { apiErrorFactory, NullError, InvalidProgramStateError } from './errors'
Expand All @@ -24,6 +25,19 @@ interface HttpClientRequestParams {
headers?: Headers
}

interface BigInt {
/** Convert to BigInt to string form in JSON.stringify */
toJSON: () => string
}

if (typeof BigInt !== 'undefined') {
Object.assign(BigInt.prototype, {
toJSON: function () {
return jsonBigInt().stringify(this)
},
})
}

export class HttpClient {
private get headers(): Headers {
const headers: Headers = {
Expand All @@ -49,6 +63,12 @@ export class HttpClient {

public readonly httpStatus = HttpStatus

private readonly json = jsonBigInt({
alwaysParseAsBig: true,
storeAsString: true,
useNativeBigInt: true,
})

public constructor(
private readonly uri: string,
private readonly auth: HttpClientAuth,
Expand All @@ -64,6 +84,24 @@ export class HttpClient {
data: params.body,
maxRedirects: 0,
validateStatus: () => true,
transformResponse: (res) => {
if (typeof res === 'string') {
try {
return this.json.parse(res)
} catch {
return res
}
}

return res
},
transformRequest: (req) => {
if (typeof req === 'object') {
return this.json.stringify(req)
}

return req
},
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/operations/ad-groups/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DateFromNumber } from 'io-ts-types/lib/DateFromNumber'
import { CampaignId, CampaignIds } from '../campaigns/types'
import { ListPagination } from '../commons/types'

export const AdGroupId = t.number
export const AdGroupId = t.string
export type AdGroupId = t.TypeOf<typeof AdGroupId>

export const AdGroupIds = t.array(AdGroupId)
Expand Down
6 changes: 3 additions & 3 deletions src/operations/campaigns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CampaignBiddingAdjustments } from '../bidding/campaign-bidding-adjustme
/**
* The ID of the campaign.
*/
export const CampaignId = t.number
export const CampaignId = t.string
export type CampaignId = t.TypeOf<typeof CampaignId>

export const CampaignIds = t.array(CampaignId)
Expand Down Expand Up @@ -516,7 +516,7 @@ export const SponsoredBrandsCampaignResponse = t.intersection([
/**
* The identifier of the ad group.
*/
adGroupId: t.number,
adGroupId: t.string,
}),
ResponseStatus,
]),
Expand All @@ -528,7 +528,7 @@ export const SponsoredBrandsCampaignResponse = t.intersection([
/**
* The identifier of the keyword.
*/
keywordId: t.number,
keywordId: t.string,
}),
ResponseStatus,
]),
Expand Down
2 changes: 1 addition & 1 deletion src/operations/drafts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SponsoredBrandsKeywordResponse,
} from '../keywords/types'

export const SponsoredBrandsDraftCampaignId = t.number
export const SponsoredBrandsDraftCampaignId = t.string
export type SponsoredBrandsDraftCampaignId = t.TypeOf<typeof SponsoredBrandsDraftCampaignId>

export const SponsoredBrandsDraftCampaignName = t.string
Expand Down
6 changes: 3 additions & 3 deletions src/operations/keywords/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import * as t from './types'
describe('Keyword', () => {
it('should pass getBiddableKeyword response', () => {
const res = t.Keyword.decode({
keywordId: 16577721726418,
adGroupId: 149522344269714,
campaignId: 164069484151709,
keywordId: '16577721726418',
adGroupId: '149522344269714',
campaignId: '164069484151709',
keywordText: 'Apple',
matchType: 'broad',
state: 'paused',
Expand Down
2 changes: 1 addition & 1 deletion src/operations/keywords/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CampaignId, CampaignIds } from '../campaigns/types'
import { DateFromNumber } from 'io-ts-types/lib/DateFromNumber'
import { AdGroupId, AdGroupIds, AdGroupState } from '../ad-groups/types'

export const KeywordId = t.number
export const KeywordId = t.string
export type KeywordId = t.TypeOf<typeof KeywordId>

export const KeywordIds = t.array(KeywordId)
Expand Down
2 changes: 1 addition & 1 deletion src/operations/negative-targeting/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { DateFromNumber } from 'io-ts-types/lib/DateFromNumber'
import { CampaignId, CampaignIds } from '../campaigns/types'
import { ListPagination } from '../commons/types'

export const NegativeTargetId = t.number
export const NegativeTargetId = t.string
export type NegativeTargetId = t.TypeOf<typeof NegativeTargetId>

export const SponsoredDisplayNegativeTargetingExpressionQueryType = t.union([
Expand Down
2 changes: 1 addition & 1 deletion src/operations/portfolios/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as t from 'io-ts'
import { AmazonMarketplaceAdvertisingCurrencyType } from '../commons/types'
import { DateFromNumber } from 'io-ts-types/lib/DateFromNumber'

export const PortfolioId = t.number
export const PortfolioId = t.string
export type PortfolioId = t.TypeOf<typeof PortfolioId>

const PortfolioName = t.string
Expand Down
2 changes: 1 addition & 1 deletion src/operations/product-ads/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AdGroupId, AdGroupIds } from '../ad-groups/types'
import { ListPagination } from '../commons/types'
import { DateFromNumber } from 'io-ts-types/lib/DateFromNumber'

export const AdId = t.number
export const AdId = t.string
export type AdId = t.TypeOf<typeof AdId>

export const AdIds = t.array(AdId)
Expand Down
6 changes: 3 additions & 3 deletions src/operations/product-targeting/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CampaignId, CampaignIds } from '../campaigns/types'
import { AdGroupId, AdGroupIds } from '../ad-groups/types'
import { DateFromNumber } from 'io-ts-types/lib/DateFromNumber'

export const TargetId = t.number
export const TargetId = t.string
export type TargetId = t.TypeOf<typeof TargetId>

export const TargetIds = t.array(TargetId)
Expand Down Expand Up @@ -187,7 +187,7 @@ export const ProductRecommendationResponse = t.strict({
})
export type ProductRecommendationResponse = t.TypeOf<typeof ProductRecommendationResponse>

export const CategoryId = t.number
export const CategoryId = t.string
export type CategoryId = t.TypeOf<typeof CategoryId>

export const CategoryResponse = t.strict({
Expand Down Expand Up @@ -227,7 +227,7 @@ export const AgeRange = t.strict({
export type AgeRange = t.TypeOf<typeof AgeRange>
export const AgeRanges = t.array(AgeRange)

export const BrandId = t.number
export const BrandId = t.string
export type BrandId = t.TypeOf<typeof BrandId>

export const BrandResponse = t.strict({
Expand Down
2 changes: 1 addition & 1 deletion src/operations/profiles/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('ProfileResponse', () => {
describe('Profile', () => {
it('should pass', () => {
const res = t.Profile.decode({
profileId: 2984328618318898,
profileId: '2984328618318898',
countryCode: 'US',
currencyCode: 'USD',
dailyBudget: 340,
Expand Down
2 changes: 1 addition & 1 deletion src/operations/profiles/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AmazonMarketplaceAdvertisingCountryCodeType,
} from '../commons/types'

export const ProfileId = t.number
export const ProfileId = t.string
export type ProfileId = t.TypeOf<typeof ProfileId>

export const ProfileResponse = t.intersection([
Expand Down
2 changes: 1 addition & 1 deletion src/operations/recommendations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type SponsoredBrandsCategoryRecommendationsRequest = t.TypeOf<
typeof SponsoredBrandsCategoryRecommendationsRequest
>

export const SponsoredBrandsCategoryId = t.number
export const SponsoredBrandsCategoryId = t.string
export type SponsoredBrandsCategoryId = t.TypeOf<typeof SponsoredBrandsCategoryId>

const SponsoredBrandsCategoryResponse = t.strict({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe('SponsoredBrandsKeywordsOperation', () => {
const client = httpClientFactory()
const operationProvider = new OperationProvider(client)
const operation = operationProvider.create(SponsoredBrandsKeywordsOperation)
const CAMPAIGN_ID = 164069484151709
const AD_GROUP_ID = 149522344269714
const CAMPAIGN_ID = '164069484151709'
const AD_GROUP_ID = '149522344269714'
const KEYWORD_ID = 123
const KEYWORD_TEXT = 'Pear'
const BID = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe('SponsoredBrandsNegativeKeywordsOperation', () => {
const client = httpClientFactory()
const operationProvider = new OperationProvider(client)
const operation = operationProvider.create(SponsoredBrandsNegativeKeywordsOperation)
const MANUAL_CAMPAIGN_ID = 164069484151709
const MANUAL_AD_GROUP_ID = 149522344269714
const MANUAL_CAMPAIGN_ID = '164069484151709'
const MANUAL_AD_GROUP_ID = '149522344269714'
const KEYWORD_ID = 123
const KEYWORD_TEXT = 'green apple'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ describe('SponsoredProductsAdGroupNegativeKeywordsOperation', () => {
const client = httpClientFactory()
const operationProvider = new OperationProvider(client)
const operation = operationProvider.create(SponsoredProductsAdGroupNegativeKeywordsOperation)
const MANUAL_CAMPAIGN_ID = 164069484151709
const MANUAL_AD_GROUP_ID = 149522344269714
const KEYWORD_ID = 262433850080632
const MANUAL_CAMPAIGN_ID = '164069484151709'
const MANUAL_AD_GROUP_ID = '149522344269714'
const KEYWORD_ID = '262433850080632'
const KEYWORD_TEXT = 'green apple'

describe('getNegativeKeyword', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe('SponsoredProductsCampaignNegativeKeywordsOperation', () => {
const client = httpClientFactory()
const operationProvider = new OperationProvider(client)
const operation = operationProvider.create(SponsoredProductsCampaignNegativeKeywordsOperation)
const MANUAL_CAMPAIGN_ID = 164069484151709
const KEYWORD_ID = 271800073719731
const MANUAL_CAMPAIGN_ID = '164069484151709'
const KEYWORD_ID = '271800073719731'
const KEYWORD_TEXT = 'banana'

describe('createCampaignNegativeKeywords', () => {
Expand Down Expand Up @@ -96,8 +96,7 @@ describe('SponsoredProductsCampaignNegativeKeywordsOperation', () => {

describe('listCampaignNegativeKeywordsExtended', () => {
it(`should return an array of campaign negative keywords extendeds`, async () => {
const res: CampaignNegativeKeywordExtended[] =
await operation.listCampaignNegativeKeywordsExtended()
const res: CampaignNegativeKeywordExtended[] = await operation.listCampaignNegativeKeywordsExtended()

expect(Array.isArray(res)).toBeTruthy()
})
Expand Down
Loading