This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parse, transform and validate response from sellers endpoint
- Loading branch information
Showing
5 changed files
with
124 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** A collection of parsing utils */ | ||
|
||
export const ensureArray = <T>(x: T | T[]): T[] => (Array.isArray(x) ? x : [x]) | ||
|
||
export const parseBoolean = (x: 'Yes' | 'No'): boolean => { | ||
switch (x) { | ||
case 'Yes': | ||
return true | ||
case 'No': | ||
return false | ||
default: | ||
throw new Error('TODO, but should be library-specific error') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,106 @@ | ||
import { Type } from '@sinclair/typebox' | ||
import Ajv from 'ajv' | ||
|
||
import { HttpClient, Resource } from '../http' | ||
import { ensureArray, parseBoolean } from '../parsing' | ||
|
||
interface MarketplaceParticipations { | ||
interface MarketplaceParticipationsResponse { | ||
ListMarketplaceParticipationsResponse: { | ||
ListMarketplaceParticipationsResult: [ | ||
{ | ||
NextToken: [string] | ||
ListParticipations: Array<{ | ||
Participation: [ | ||
{ | ||
MarketplaceId: [string] | ||
SellerId: [string] | ||
HasSellerSuspendedListings: [string] | ||
}, | ||
] | ||
ListMarketplaceParticipationsResult: { | ||
NextToken: string | ||
ListParticipations: { | ||
Participation: Array<{ | ||
MarketplaceId: string | ||
SellerId: string | ||
HasSellerSuspendedListings: 'Yes' | 'No' | ||
}> | ||
ListMarketplaces: Array<{ | ||
Marketplace: [ | ||
{ | ||
MarketplaceId: [string] | ||
Name: [string] | ||
DefaultCountryCode: [string] | ||
DefaultCurrencyCode: [string] | ||
DefaultLanguageCode: [string] | ||
DomainName: [string] | ||
}, | ||
] | ||
} | ||
ListMarketplaces: { | ||
Marketplace: Array<{ | ||
MarketplaceId: string | ||
Name: string | ||
DefaultCountryCode: string | ||
DefaultCurrencyCode: string | ||
DefaultLanguageCode: string | ||
DomainName: string | ||
}> | ||
}, | ||
] | ||
ResponseMetadata: [{ RequestId: [string] }] | ||
} | ||
} | ||
ResponseMetadata: { RequestId: string } | ||
} | ||
} | ||
|
||
const MarketplaceParticipations = Type.Object({ | ||
participations: Type.Array( | ||
Type.Object({ | ||
marketplaceId: Type.String(), | ||
sellerId: Type.String(), | ||
hasSellerSuspendedListings: Type.Boolean(), | ||
}), | ||
), | ||
marketplaces: Type.Array( | ||
Type.Object({ | ||
marketplaceId: Type.String(), | ||
name: Type.String(), | ||
defaultCountryCode: Type.String(), | ||
defaultCurrencyCode: Type.String(), | ||
defaultLanguageCode: Type.String(), | ||
domainName: Type.String(), | ||
}), | ||
), | ||
}) | ||
|
||
// Unfortunately typebox creates an ugly type from Static<typeof X> (not a regular object with keys), | ||
// it's not something that we can expose from this library, so I've added a manually created interface | ||
interface MarketplaceParticipations { | ||
participations: Array<{ | ||
marketplaceId: string | ||
sellerId: string | ||
hasSellerSuspendedListings: boolean | ||
}> | ||
marketplaces: Array<{ | ||
marketplaceId: string | ||
name: string | ||
defaultCountryCode: string | ||
defaultCurrencyCode: string | ||
defaultLanguageCode: string | ||
domainName: string | ||
}> | ||
} | ||
|
||
export class Sellers { | ||
constructor(private httpClient: HttpClient) {} | ||
|
||
// TODO: transform response | ||
async listMarketplaceParticipations() { | ||
return this.httpClient.request('POST', { | ||
async listMarketplaceParticipations(): Promise<MarketplaceParticipations> { | ||
const response: MarketplaceParticipationsResponse = await this.httpClient.request('POST', { | ||
resource: Resource.Sellers, | ||
version: '2011-07-01', | ||
action: 'ListMarketplaceParticipations', | ||
parameters: {}, | ||
}) | ||
|
||
const data = response.ListMarketplaceParticipationsResponse.ListMarketplaceParticipationsResult | ||
|
||
const result: MarketplaceParticipations = { | ||
participations: ensureArray(data.ListParticipations.Participation).map((x) => ({ | ||
marketplaceId: x.MarketplaceId, | ||
sellerId: x.SellerId, | ||
hasSellerSuspendedListings: parseBoolean(x.HasSellerSuspendedListings), | ||
})), | ||
marketplaces: ensureArray(data.ListMarketplaces.Marketplace).map((x) => ({ | ||
marketplaceId: x.MarketplaceId, | ||
name: x.Name, | ||
defaultCountryCode: x.DefaultCountryCode, | ||
defaultCurrencyCode: x.DefaultCurrencyCode, | ||
defaultLanguageCode: x.DefaultLanguageCode, | ||
domainName: x.DomainName, | ||
})), | ||
} | ||
|
||
if (new Ajv().validate(MarketplaceParticipations, result)) { | ||
return result | ||
} | ||
|
||
throw new Error('TODO for now') | ||
} | ||
} |