Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
feat: parse xml response
Browse files Browse the repository at this point in the history
  • Loading branch information
gigobyte committed May 3, 2020
1 parent 0b90aac commit dd184cd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
12 changes: 7 additions & 5 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"types": "lib/index.d.ts",
"dependencies": {
"@scaleleap/amazon-marketplaces": "^4.0.2",
"axios": "^0.19.2"
"@sinclair/typebox": "^0.9.13",
"axios": "^0.19.2",
"xml2js": "^0.4.23"
},
"devDependencies": {
"@scaleleap/config": "1.6.1",
Expand Down
20 changes: 17 additions & 3 deletions src/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AmazonMarketplace } from '@scaleleap/amazon-marketplaces'
import axios from 'axios'
import { URLSearchParams } from 'url'
import { parseString } from 'xml2js'

import { sign } from './sign'

Expand Down Expand Up @@ -46,16 +47,29 @@ const canonicalizeParameters = (parameters: Parameters): string => {
return sp.toString().replace(/\+/g, '%20')
}

const defaultFetch = ({ url, method, headers, data }: Request) =>
axios({ method, url, headers, data }).then((response) => response.data)
const defaultFetch = <T>({ url, method, headers, data }: Request) =>
axios({ method, url, headers, data }).then(
(response) =>
new Promise<T>((resolve, reject) =>
parseString(response.data, (error, result) => {
if (error) {
reject(error)
}
resolve(result)
}),
),
)

export class HttpClient {
constructor(
private options: MWSOptions,
private fetch: <T>(meta: Request) => Promise<T> = defaultFetch,
) {}

request<TResource extends Resource>(method: HttpMethod, info: ResourceInfo<TResource>) {
request<TResource extends Resource, TRes>(
method: HttpMethod,
info: ResourceInfo<TResource>,
): Promise<TRes> {
const marketplaceUri = this.options.marketplace.webServiceUri

const host = marketplaceUri.replace('https://', '')
Expand Down
36 changes: 34 additions & 2 deletions src/sections/sellers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
import { HttpClient, Resource } from '../http'

interface MarketplaceParticipations {
ListMarketplaceParticipationsResponse: {
ListMarketplaceParticipationsResult: [
{
NextToken: [string]
ListParticipations: Array<{
Participation: [
{
MarketplaceId: [string]
SellerId: [string]
HasSellerSuspendedListings: [string]
},
]
}>
ListMarketplaces: Array<{
Marketplace: [
{
MarketplaceId: [string]
Name: [string]
DefaultCountryCode: [string]
DefaultCurrencyCode: [string]
DefaultLanguageCode: [string]
DomainName: [string]
},
]
}>
},
]
ResponseMetadata: [{ RequestId: [string] }]
}
}

export class Sellers {
constructor(private httpClient: HttpClient) {}

// TODO: type and parse response
listMarketplaceParticipations() {
// TODO: transform response
async listMarketplaceParticipations() {
return this.httpClient.request('POST', {
resource: Resource.Sellers,
version: '2011-07-01',
Expand Down

0 comments on commit dd184cd

Please sign in to comment.