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

Commit

Permalink
fix: fix codec to use ensure array
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed May 28, 2020
1 parent d52262e commit cbc567a
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/sections/products.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
array,
boolean,
Codec,
exactly,
Expand Down Expand Up @@ -199,21 +198,16 @@ const MatchingProductForId = Codec.interface({
Products: ensureArray('Product', Product),
})

// GetMatchingProductForId is different than other APIs,
// The example on MWS points to it possibly being an array structured this way
const GetMatchingProductForIdResult = oneOf([
const GetMatchingProductForIdResponse = ensureArray(
'GetMatchingProductForIdResult',
MatchingProductForId,
array(MatchingProductForId),
string,
])
)

const GetMatchingProductForIdResponse = Codec.interface({
GetMatchingProductForIdResponse: Codec.interface({
GetMatchingProductForIdResult,
}),
const GetMatchingProductForIdResponseCodec = Codec.interface({
GetMatchingProductForIdResponse,
})

type GetMatchingProductForIdResult = GetInterface<typeof GetMatchingProductForIdResult>
type GetMatchingProductForIdResponse = GetInterface<typeof GetMatchingProductForIdResponse>

export class Products {
constructor(private httpClient: HttpClient) {}
Expand Down Expand Up @@ -281,16 +275,16 @@ export class Products {

async getMatchingProductForId(
parameters: GetMatchingProductForIdParameters,
): Promise<[GetMatchingProductForIdResult, RequestMeta]> {
): Promise<[GetMatchingProductForIdResponse, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Products,
version: PRODUCTS_API_VERSION,
action: 'GetMatchingProductForId',
parameters,
})

return GetMatchingProductForIdResponse.decode(response).caseOf({
Right: (x) => [x.GetMatchingProductForIdResponse.GetMatchingProductForIdResult, meta],
return GetMatchingProductForIdResponseCodec.decode(response).caseOf({
Right: (x) => [x.GetMatchingProductForIdResponse, meta],
Left: (error) => {
throw new ParsingError(error)
},
Expand Down

0 comments on commit cbc567a

Please sign in to comment.