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

Commit

Permalink
fix: specify valid formats, make xml default if invalid format
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed Aug 10, 2020
1 parent 0a5ead1 commit 3c703da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
22 changes: 10 additions & 12 deletions src/sections/feeds.ts
Expand Up @@ -15,7 +15,7 @@ import {
unknown,
} from 'purify-ts'

import { InvalidParameterValue, ParsingError } from '../error'
import { ParsingError } from '../error'
import { HttpClient, RequestMeta, Resource } from '../http'
import { ensureString, mwsDate, NextToken, nextToken as nextTokenCodec } from '../parsing'

Expand Down Expand Up @@ -169,7 +169,7 @@ const GetFeedSubmissionResultResponse = Codec.custom<string>({

export interface GetFeedSubmissionResultParameters {
FeedSubmissionId: string
format?: string
format?: 'xml' | 'json'
}

export interface SubmitFeedParameters {
Expand Down Expand Up @@ -228,7 +228,7 @@ export class Feeds {
async getFeedSubmissionResult(
parameters: GetFeedSubmissionResultParameters,
): Promise<[FeedSubmission | Record<string, unknown>, RequestMeta] | void> {
const stringResponse = parameters.format === 'xml'
const stringResponse = parameters.format === 'xml' || !parameters.format

const [response, meta] = await this.httpClient.request(
'POST',
Expand All @@ -243,14 +243,7 @@ export class Feeds {
'',
stringResponse,
)
if (stringResponse) {
return GetFeedSubmissionResultResponse.decode(response).caseOf({
Right: (x) => [x, meta],
Left: (error) => {
throw new ParsingError(error)
},
})
}

if (parameters.format === 'json') {
return record(string, unknown)
.decode(response)
Expand All @@ -262,7 +255,12 @@ export class Feeds {
})
}

throw new InvalidParameterValue('"format" parameter is incorrect')
return GetFeedSubmissionResultResponse.decode(response).caseOf({
Right: (x) => [x, meta],
Left: (error) => {
throw new ParsingError(error)
},
})
}

async cancelFeedSubmissions(
Expand Down
21 changes: 3 additions & 18 deletions test/unit/feeds.test.ts
@@ -1,4 +1,4 @@
import { InvalidParameterValue, ParsingError, SubmitFeedParameters } from '../../src'
import { ParsingError, SubmitFeedParameters } from '../../src'
import { NextToken } from '../../src/parsing'
import { createMockHttpClient, mockMwsFail, mockParsingError, parsingErrorRegex } from '../utils'

Expand Down Expand Up @@ -29,17 +29,12 @@ describe('feeds', () => {
describe('getFeedSubmissionResult', () => {
const xmlParameters = {
FeedSubmissionId: '',
format: 'xml',
format: 'xml' as 'xml',
}

const jsonParameters = {
FeedSubmissionId: '',
format: 'json',
}

const incorrectParameters = {
FeedSubmissionId: '',
format: 'yml',
format: 'json' as 'json',
}

it('returns an XML file when succesful', async () => {
Expand All @@ -62,16 +57,6 @@ describe('feeds', () => {
).toMatchSnapshot()
})

it('throws invalid parameters error if format parameter is incorrect', async () => {
expect.assertions(1)

const mockGetFeedSubmissionResult = createMockHttpClient('feeds_get_feed_submission_result')

await expect(() =>
mockGetFeedSubmissionResult.feeds.getFeedSubmissionResult(incorrectParameters),
).rejects.toStrictEqual(new InvalidParameterValue('"format" parameter is incorrect'))
})

it('throws a parsing error when the response isnt valid', async () => {
expect.assertions(1)

Expand Down

0 comments on commit 3c703da

Please sign in to comment.