Skip to content

Commit

Permalink
feat: adds HTTP client
Browse files Browse the repository at this point in the history
  • Loading branch information
moltar authored and nguyentoanit committed Oct 19, 2019
1 parent f13694c commit 8a45622
Show file tree
Hide file tree
Showing 12 changed files with 636 additions and 2 deletions.
47 changes: 47 additions & 0 deletions package-lock.json

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

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"build": "tsc",
"lint": "eslint --ext .js,.ts src test",
"test": "jest",
"test:passthrough": "cross-env POLLY_MODE=passthrough npm test -- -t 'polly:passthrough'"
"test:passthrough": "cross-env POLLY_MODE=passthrough npm test -- -t 'polly:passthrough'",
"dev:refresh-token": "ts-node test/refresh-token.ts"
},
"keywords": [],
"author": "",
Expand All @@ -16,9 +17,11 @@
"client-oauth2": "4.2.5",
"cross-fetch": "3.0.4",
"fp-ts": "2.1.0",
"http-status-codes": "1.3.2",
"io-ts": "2.0.1",
"io-ts-types": "0.5.1",
"lodash": "4.17.15"
"lodash": "4.17.15",
"ts-error": "1.0.3"
},
"devDependencies": {
"@pollyjs/adapter-node-http": "2.6.3",
Expand All @@ -40,7 +43,9 @@
"eslint-plugin-prettier": "3.1.1",
"jest": "24.9.0",
"prettier": "1.18.2",
"setup-polly-jest": "0.6.0",
"ts-jest": "24.1.0",
"ts-node": "8.4.1",
"tsconfigs": "4.0.1",
"typescript": "3.6.4"
}
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { name, version } from '../package.json'

export const USER_AGENT = `${name}/${version}`

export const JSON_CONTENT_TYPE = 'application/json'
79 changes: 79 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (C) 2019, Scale Leap
*/

import { ExtendableError } from 'ts-error'

export interface ErrorObject {
code: string
details: string
requestId: string
}

export class NullError extends ExtendableError {
public constructor(resource: string) {
super(`Response result is null for "${resource}".`)
}
}

export class InvalidProgramStateError extends ExtendableError {
public constructor(additionalDetails?: string) {
super(
[
'This program state should never happen.',
'If you encountered this error, please report it asap.',
additionalDetails,
].join(' '),
)
}
}

export class SnapshotDownloadError extends ExtendableError {
public constructor(snapshotId: string, snapshotStatus: string) {
super(
[
'Snapshot must have status equal to SUCCESS before downloading.',
`Got snapshot ${snapshotId} with status ${snapshotStatus} instead.`,
].join(' '),
)
}
}

export class GenericError extends ExtendableError {
public code: string

public requestId: string

public constructor(err: ErrorObject) {
super(err.details)
this.code = err.code
this.requestId = err.requestId
}
}

export class UnauthorizedError extends GenericError {}

export class BadRequestError extends GenericError {}

export class UnprocessableEntityError extends GenericError {}

export class ResourceNotFoundError extends GenericError {}

export class NotAcceptableError extends GenericError {}

export function apiErrorFactory(err: ErrorObject): GenericError {
switch (err.code) {
case 'UNAUTHORIZED':
return new UnauthorizedError(err)
case 'NOT_FOUND':
return new ResourceNotFoundError(err)
case '400':
return new BadRequestError(err)
case '406':
return new NotAcceptableError(err)
case '422':
return new UnprocessableEntityError(err)
default:
return new GenericError(err)
}
}
12 changes: 12 additions & 0 deletions src/gunzip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { gunzip } from 'zlib'

export default (buffer: Buffer): Promise<Buffer> => {
return new Promise((resolve, reject) => {
return gunzip(buffer, (err, uncompressed) => {
if (err) {
return reject(err)
}
return resolve(uncompressed)
})
})
}
Loading

0 comments on commit 8a45622

Please sign in to comment.