Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds HTTP client #33

Merged
merged 8 commits into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ on:

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [8.x, 10.x, 12.x]
node-version:
# - 8.x
# - 10.x
- 12.x

steps:
- uses: actions/checkout@v1
Expand All @@ -22,6 +24,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: npm install, lint, test and build
run: |
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
npm install
npm run lint
npm test
Expand All @@ -35,3 +38,8 @@ jobs:
TEST_REFRESH_TOKEN: ${{ secrets.TEST_REFRESH_TOKEN }}
TEST_SCOPE: ${{ secrets.TEST_SCOPE }}
CI: true
- name: push
uses: github-actions-x/commit@v2.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
commit-message: 'test: adds Polly recordings'
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