Skip to content

Commit

Permalink
fix: api configuration parameters's properties
Browse files Browse the repository at this point in the history
* remove apiKey, formDataCtor
* add explicitly set for basePath
* remove apiModelProperties
  • Loading branch information
nguyentoanit committed Mar 17, 2021
1 parent e21d3d6 commit dc2da96
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 70 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ CLIENT_SECRET=
REFRESH_TOKEN=

## set to your preference
API_URL=https://sellingpartnerapi-eu.amazon.com
API_REGION=eu-west-1
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ assertMarketplaceHasSellingPartner(CA)

const configuration: APIConfigurationParameters = {
accessToken: 'Atza|...',
apiModelProperties: { region: CA.sellingPartner.region },
region: CA.sellingPartner.region,
}

const client = new UploadsApiClient(configuration)
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/api-client-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ApiClientHelpers {
if (axios) {
axiosInstance = axios
} else {
const { apiModelProperties, accessToken } = parameters
const { accessToken, credentials, region } = parameters

axiosInstance = globalAxios.create({
headers: {
Expand All @@ -25,10 +25,10 @@ export class ApiClientHelpers {
axiosInstance.interceptors.request.use(
aws4Interceptor(
{
region: apiModelProperties.region.awsRegion,
region: region.awsRegion,
service: 'execute-api',
},
apiModelProperties.credentials,
credentials,
),
)
}
Expand Down
15 changes: 15 additions & 0 deletions src/types/api-clients/amazon-selling-partner-api-base-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const enum ProductionApiBasePath {
NA = 'https://sellingpartnerapi-na.amazon.com',
EU = 'https://sellingpartnerapi-eu.amazon.com',
FE = 'https://sellingpartnerapi-fe.amazon.com',
}

export const enum SandboxApiBasePath {
NA = 'https://sandbox.sellingpartnerapi-na.amazon.com',
EU = 'https://sandbox.sellingpartnerapi-eu.amazon.com',
FE = 'https://sandbox.sellingpartnerapi-fe.amazon.com',
}

export type ApiBasePath = ProductionApiBasePath | SandboxApiBasePath

export const DEFAULT_API_BASE_PATH = ProductionApiBasePath.NA
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface SellingPartnerAPICredentials {
export interface AmazonSellingPartnerAPICredentials {
accessKeyId: string
secretAccessKey: string
sessionToken?: string
Expand Down
57 changes: 22 additions & 35 deletions src/types/api-clients/api-configuration-parameters.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
import { AxiosInstance, AxiosRequestConfig } from 'axios'

import { SellingPartnerApiModelProperties } from './selling-partner-api-model-properties'
import { ApiBasePath } from './amazon-selling-partner-api-base-path'
import { AmazonSellingPartnerAPICredentials } from './amazon-selling-partner-api-credentials'
import { AmazonSellingPartnerApiRegion } from './amazon-selling-partner-api-region'

export interface APIConfigurationParameters {
/**
* parameter for apiKey security
* @param name security name
* @memberof APIConfigurationParameters
*/
apiKey?:
| string
| Promise<string>
| ((name: string) => string)
| ((name: string) => Promise<string>)

/**
* Selling Partner API model properties.
* Use for signing requests.
*
* @type {SellingPartnerApiModelProperties}
* @memberof APIConfigurationParameters
*/
apiModelProperties: SellingPartnerApiModelProperties

/**
* Axios Instance
*
Expand All @@ -33,35 +15,40 @@ export interface APIConfigurationParameters {

/**
* parameter for oauth2 security
* @param name security name
* @param scopes oauth2 scope
*
* @memberof APIConfigurationParameters
*/
accessToken?:
| string
| Promise<string>
| ((name?: string, scopes?: string[]) => string)
| ((name?: string, scopes?: string[]) => Promise<string>)
accessToken?: string

/**
* override base path
*
* @type {string}
* @type {ApiBasePath}
* @memberof APIConfigurationParameters
*/
basePath?: string
basePath?: ApiBasePath

/**
* base options for axios calls
*
* @type {AxiosRequestConfig}
* @memberof APIConfigurationParameters
*/
baseOptions?: AxiosRequestConfig

/**
* The FormData constructor that will be used to create multipart form data
* requests. You can inject this here so that execution environments that
* do not support the FormData class can still run the generated client.
* Selling partner API credentials
*
* @type {new () => FormData}
* @type {AmazonSellingPartnerAPICredentials}
* @memberof APIConfigurationParameters
*/
credentials?: AmazonSellingPartnerAPICredentials

/**
* Selling partner API region
*
* @type {AmazonSellingPartnerApiRegion}
* @memberof APIConfigurationParameters
*/
formDataCtor?: new () => FormData
region: AmazonSellingPartnerApiRegion
}
1 change: 0 additions & 1 deletion src/types/api-clients/default-api-base-path.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/types/api-clients/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './amazon-selling-partner-api-credentials'
export * from './amazon-selling-partner-api-region'
export * from './api-configuration-parameters'
export * from './default-api-base-path'
export * from './selling-partner-api-credentials'
export * from './selling-partner-api-model-properties'
export * from './amazon-selling-partner-api-base-path'
7 changes: 0 additions & 7 deletions src/types/api-clients/selling-partner-api-model-properties.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/api-clients/uploads-api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe(`${UploadsApiClient.name}`, () => {

const configuration: APIConfigurationParameters = {
accessToken: 'Atza|...',
apiModelProperties: { region: CA.sellingPartner.region },
region: CA.sellingPartner.region,
}

const client = new UploadsApiClient(configuration)
Expand Down
7 changes: 0 additions & 7 deletions test/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ export const CLIENT_SECRET = env.get('CLIENT_SECRET').required().asString()

export const REFRESH_TOKEN = env.get('REFRESH_TOKEN').required().asString()

export const API_URL = env
.get('API_URL')
.default('https://sellingpartnerapi-na.amazon.com')
.required()
// cannot use asUriString: https://github.com/evanshortiss/env-var/issues/126
.asString()

export const API_REGION = env.get('API_REGION').default('us-east-1').required().asString()

export const ROLE_ARN = env.get('ROLE_ARN').required().asString()
19 changes: 9 additions & 10 deletions test/integration/sellers-api-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AssumeRoleCommand, STSClient } from '@aws-sdk/client-sts'
import axios from 'axios'

import { SandboxApiBasePath } from '../../src'
import { SellersApiClient } from '../../src/api-clients/sellers-api-client'
import * as environment from '../environment'

Expand Down Expand Up @@ -44,17 +45,15 @@ describe(`${SellersApiClient.name}`, () => {
)

const client = new SellersApiClient({
basePath: environment.API_URL,
basePath: SandboxApiBasePath.EU,
accessToken: tokens.access_token,
apiModelProperties: {
region: {
awsRegion: environment.API_REGION,
},
credentials: {
accessKeyId: Credentials?.AccessKeyId || '',
secretAccessKey: Credentials?.SecretAccessKey || '',
sessionToken: Credentials?.SessionToken || '',
},
region: {
awsRegion: environment.API_REGION,
},
credentials: {
accessKeyId: Credentials?.AccessKeyId || '',
secretAccessKey: Credentials?.SecretAccessKey || '',
sessionToken: Credentials?.SessionToken || '',
},
})

Expand Down

0 comments on commit dc2da96

Please sign in to comment.