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

Commit

Permalink
feat: have a special type for nexttoken
Browse files Browse the repository at this point in the history
  • Loading branch information
gigobyte committed May 8, 2020
1 parent e460ecd commit 458ab03
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/parsing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** A collection of parsing codecs */
import { array, Codec } from 'purify-ts/Codec'
import { array, Codec, string } from 'purify-ts/Codec'
import { Left, Right } from 'purify-ts/Either'

export const ensureArray = <T>(codec: Codec<T>): Codec<T[]> => {
Expand Down Expand Up @@ -62,3 +62,14 @@ export const serviceStatus = Codec.custom<ServiceStatus>({
encode: (x) => x,
schema: () => ({ type: 'string', enum: ['GREEN', 'YELLOW', 'RED'] }),
})

export class NextToken<T> {
constructor(private action: T, public token: string) {}
}

export const nextToken = <T extends string>(action: T) =>
Codec.custom<NextToken<T>>({
decode: (x) => string.decode(x).map((string_) => new NextToken(action, string_)),
encode: (x) => x,
schema: () => ({ type: 'string' }),
})
14 changes: 10 additions & 4 deletions src/sections/sellers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import { Codec, GetInterface, optional, string } from 'purify-ts'

import { ParsingError } from '../error'
import { HttpClient, RequestMeta, Resource } from '../http'
import { ensureArray, mwsBoolean, serviceStatus } from '../parsing'
import {
ensureArray,
mwsBoolean,
NextToken,
nextToken as nextTokenCodec,
serviceStatus,
} from '../parsing'

const SELLERS_API_VERSION = '2011-07-01'

const MarketplaceParticipations = Codec.interface({
NextToken: optional(string),
NextToken: optional(nextTokenCodec('ListMarketplaceParticipations')),
ListParticipations: Codec.interface({
Participation: ensureArray(
Codec.interface({
Expand Down Expand Up @@ -80,13 +86,13 @@ export class Sellers {
}

async listMarketplaceParticipationsByNextToken(
nextToken: string,
nextToken: NextToken<'ListMarketplaceParticipations'>,
): Promise<[MarketplaceParticipations, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Sellers,
version: SELLERS_API_VERSION,
action: 'ListMarketplaceParticipationsByNextToken',
parameters: { NextToken: nextToken },
parameters: { NextToken: nextToken.token },
})

return MarketplaceParticipationsByNextTokenResponse.decode(response).caseOf({
Expand Down
17 changes: 16 additions & 1 deletion test/unit/parsing.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { number, Right } from 'purify-ts'

import { ensureArray, mwsBoolean, ServiceStatus, serviceStatus } from '../../src/parsing'
import {
ensureArray,
mwsBoolean,
NextToken,
nextToken,
ServiceStatus,
serviceStatus,
} from '../../src/parsing'

describe('ensureArray', () => {
it('acts like an idenity if the value to be decoded is already an array', () => {
Expand Down Expand Up @@ -61,3 +68,11 @@ describe('serviceStatus', () => {
expect(serviceStatus.decode('Green')).toMatchSnapshot()
})
})

describe('nextToken', () => {
it('is a string parser that constructs a NextToken object', () => {
expect.assertions(1)

expect(nextToken('Action').decode('123')).toStrictEqual(Right(new NextToken('Action', '123')))
})
})

0 comments on commit 458ab03

Please sign in to comment.