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

Commit

Permalink
refactor: replace oneOf codec with enumeration
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed Nov 7, 2020
1 parent 43e3c68 commit d71d51f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 63 deletions.
88 changes: 44 additions & 44 deletions src/error-codec.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
import { Codec, exactly, oneOf, optional, string } from 'purify-ts/Codec'

export enum CodeEnum {
InputStreamDisconnected,
InvalidParameterValue,
AccessDenied,
InvalidAccessKeyId,
SignatureDoesNotMatch,
InvalidAddress,
InternalError,
'Internal Error',
QuotaExceeded,
RequestThrottled,
ResourceNotFound,
ScheduledPackageAlreadyExists,
RegionNotSupported,
ScheduleWindowExpired,
InvalidOrderState,
PickupSlotNotAvailable,
AccessToFeedProcessingResultDenied,
ContentMD5Missing,
ContentMD5DoesNotMatch,
FeedCanceled,
FeedProcessingResultNoLongerAvailable,
FeedProcessingResultNotReady,
InputDataError,
InvalidFeedSubmissionId,
InvalidFeedType,
InvalidRequest,
NonRetriableInternalError,
RetriableInternalError,
AccessToReportDenied,
InvalidReportId,
InvalidReportRequestId,
InvalidReportType,
InvalidScheduleFrequency,
ReportNoLongerAvailable,
ReportNotReady,
DependencyFatalException,
DependencyRetriableException,
DependencyUnauthorizedException,
InternalErrorFatalException,
InvalidInputFatalException,
}

export const Error = Codec.interface({
Type: string,
Code: oneOf(
([
'InputStreamDisconnected',
'InvalidParameterValue',
'AccessDenied',
'InvalidAccessKeyId',
'SignatureDoesNotMatch',
'InvalidAddress',
'InternalError',
'Internal Error',
'QuotaExceeded',
'RequestThrottled',
'ResourceNotFound',
'ScheduledPackageAlreadyExists',
'RegionNotSupported',
'ScheduleWindowExpired',
'InvalidOrderState',
'PickupSlotNotAvailable',
'AccessToFeedProcessingResultDenied',
'ContentMD5Missing',
'ContentMD5DoesNotMatch',
'FeedCanceled',
'FeedProcessingResultNoLongerAvailable',
'FeedProcessingResultNotReady',
'InputDataError',
'InvalidFeedSubmissionId',
'InvalidFeedType',
'InvalidRequest',
'NonRetriableInternalError',
'RetriableInternalError',
'AccessToReportDenied',
'InvalidReportId',
'InvalidReportRequestId',
'InvalidReportType',
'InvalidScheduleFrequency',
'ReportNoLongerAvailable',
'ReportNotReady',
'DependencyFatalException',
'DependencyRetriableException',
'DependencyUnauthorizedException',
'InternalErrorFatalException',
'InvalidInputFatalException',
] as const).map((element) => exactly(element)),
),
Code: oneOf(Object.keys(CodeEnum).map((x) => exactly(x)) as [Codec<string>, ...Codec<string>[]]),
Message: string,
Detail: optional(string),
})
Expand Down
2 changes: 1 addition & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-classes-per-file */
import { GetInterface } from 'purify-ts/Codec'
import { GetInterface } from 'purify-ts'
import { ExtendableError } from 'ts-error'

import { MWSApiError } from './error-codec'
Expand Down
2 changes: 1 addition & 1 deletion src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ export class HttpClient {
InvalidInputFatalException: InvalidInputFatalExceptionError,
}

const ErrorToThrow = errorMap[errorCode]
const ErrorToThrow = errorMap[errorCode as keyof typeof errorMap]

throw enhanceError(new ErrorToThrow(`${info.action} request failed`), response)
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/sections/fulfillment-inventory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Codec, exactly, GetInterface, number, oneOf, optional, string } from 'purify-ts'
import { Codec, enumeration, GetInterface, number, optional, string } from 'purify-ts'

import { ParsingError } from '../error'
import { HttpClient, RequestMeta, Resource } from '../http'
Expand Down Expand Up @@ -50,13 +50,13 @@ export enum SupplyType {
}

const Timepoint = Codec.interface({
TimepointType: oneOf(Object.values(TimepointType).map((x) => exactly(x))),
TimepointType: enumeration(TimepointType),
DateTime: optional(mwsDate),
})

const InventorySupplyDetail = Codec.interface({
Quantity: number,
SupplyType: oneOf(Object.values(SupplyType).map((x) => exactly(x))),
SupplyType: enumeration(SupplyType),
EarliestAvailableToPick: Timepoint,
LatestAvailableToPick: Timepoint,
})
Expand All @@ -65,7 +65,7 @@ const InventorySupply = Codec.interface({
SellerSKU: optional(string),
FNSKU: string,
ASIN: optional(ensureString),
Condition: optional(oneOf(Object.values(InventoryCondition).map((x) => exactly(x)))),
Condition: optional(enumeration(InventoryCondition)),
TotalSupplyQuantity: number,
InStockSupplyQuantity: number,
EarliestAvailability: optional(Timepoint),
Expand Down
19 changes: 6 additions & 13 deletions src/sections/orders.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {
boolean,
Codec,
exactly,
enumeration,
GetInterface,
number,
oneOf,
optional,
string,
} from 'purify-ts/Codec'
Expand Down Expand Up @@ -84,17 +83,11 @@ export enum ConditionSubtype {
Other = 'Other',
}

const orderStatus: Codec<OrderStatusEnum> = oneOf(
Object.values(OrderStatusEnum).map((x) => exactly(x)),
)
const fulfillmentChannel: Codec<FulfillmentChannelEnum> = oneOf(
Object.values(FulfillmentChannelEnum).map((x) => exactly(x)),
)
const adddressType: Codec<AddressType> = oneOf(Object.values(AddressType).map((x) => exactly(x)))
const condition: Codec<Condition> = oneOf(Object.values(Condition).map((x) => exactly(x)))
const conditionSubtype: Codec<ConditionSubtype> = oneOf(
Object.values(ConditionSubtype).map((x) => exactly(x)),
)
const orderStatus = enumeration(OrderStatusEnum)
const fulfillmentChannel = enumeration(FulfillmentChannelEnum)
const adddressType = enumeration(AddressType)
const condition = enumeration(Condition)
const conditionSubtype = enumeration(ConditionSubtype)

const Address = Codec.interface({
Name: optional(string),
Expand Down

0 comments on commit d71d51f

Please sign in to comment.