Skip to content

Commit

Permalink
fix upload method, remove correlation id
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed May 15, 2023
1 parent 188a402 commit 6c582a1
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 143 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.12.0
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
"dependencies": {
"axios": "^0.21.1",
"event-emitter": "^0.3.5",
"form-data": "^3.0.0",
"js-base64": "2.6.3",
"qs": "^6.10.3",
"urijs": "^1.19.7",
"uuid": "^8.3.2"
"urijs": "^1.19.7"
},
"description": "JS client for Put.io API",
"devDependencies": {
Expand Down
19 changes: 0 additions & 19 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import qs from 'qs'

import {
PutioAPIClientResponseInterceptorFactory,
PutioApiClientRequestInterceptorFactory,
IPutioAPIClientOptions,
IPutioAPIClientResponse,
} from './types'

import { DEFAULT_CLIENT_OPTIONS } from '../constants'

import { createCorrelationIdSetter } from '../interceptors/request/correlationIdSetter'
import { createClientIPChangeEmitter } from '../interceptors/response/clientIPChangeEmitter'
import { createErrorEmitter } from '../interceptors/response/errorEmitter'
import { createResponseFormatter } from '../interceptors/response/responseFormatter'
Expand Down Expand Up @@ -180,23 +178,6 @@ export class PutioAPIClient {
qs.stringify(params, { arrayFormat: 'comma' }),
})

// apply request interceptors
const requestInterceptorFactories: PutioApiClientRequestInterceptorFactory[] = [
createCorrelationIdSetter,
]

requestInterceptorFactories
.map(createInterceptor => createInterceptor(this.options))
.forEach(requestInterceptor => {
axiosInstance.interceptors.request.use(
requestInterceptor,
null,
// the 3rd argument is not reflected in the types, but it exists?
// @ts-ignore
{ synchronous: true },
)
})

// apply response interceptors
const responseInterceptorFactories: PutioAPIClientResponseInterceptorFactory[] = [
createResponseFormatter,
Expand Down
13 changes: 2 additions & 11 deletions src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
import { AxiosError, AxiosResponse } from 'axios'

export interface IPutioAPIClientOptions {
clientID?: number
baseURL?: string
webAppURL?: string
generateUUID?: () => string
}

export interface IPutioAPIClientResponse<T> extends AxiosResponse {
Expand All @@ -13,7 +12,7 @@ export interface IPutioAPIClientResponse<T> extends AxiosResponse {
}

export interface IPutioAPIClientErrorData {
correlation_id?: string
'x-trace-id'?: string
error_id?: string
error_uri?: string
error_type: string
Expand All @@ -27,14 +26,6 @@ export interface IPutioAPIClientError
toJSON: () => IPutioAPIClientErrorData
}

export type PutioAPIClientRequestInterceptor = (
config: AxiosRequestConfig,
) => AxiosRequestConfig

export type PutioApiClientRequestInterceptorFactory = (
options: IPutioAPIClientOptions,
) => PutioAPIClientRequestInterceptor

export type PutioAPIClientResponseInterceptor = {
onFulfilled: (
response: IPutioAPIClientResponse<any>,
Expand Down
2 changes: 0 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { IPutioAPIClientOptions } from './client/types'

export const CORRELATION_ID_HEADER_NAME = 'X-Putio-Correlation-Id'
export const NIL_CORRELATION_ID = '00000000-0000-0000-0000-000000000000'
export const DEFAULT_CLIENT_OPTIONS: IPutioAPIClientOptions = {
baseURL: 'https://api.put.io/v2',
clientID: 1,
Expand Down
50 changes: 0 additions & 50 deletions src/interceptors/request/correlationIdSetter.spec.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/interceptors/request/correlationIdSetter.ts

This file was deleted.

22 changes: 11 additions & 11 deletions src/interceptors/response/responseFormatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ describe('interceptors/response/responseFormatter', () => {
"foo": "bar",
"status": "OK",
},
"config": Object {
"headers": Object {
"X-Putio-Correlation-Id": "00000000-0000-0000-0000-000000000000",
},
},
"config": Object {},
"data": Object {
"foo": "bar",
"status": "OK",
},
"headers": Object {},
"headers": Object {
"x-trace-id": "MOCK_TRACE_ID",
},
"status": 200,
"statusText": "ok",
}
Expand All @@ -46,7 +44,9 @@ describe('interceptors/response/responseFormatter', () => {
error_message: 'Putio API Error',
status_code: 400,
},
headers: {},
headers: {
'x-trace-id': 'MOCK_TRACE_ID',
},
status: 400,
statusText: 'Error!',
},
Expand All @@ -55,10 +55,10 @@ describe('interceptors/response/responseFormatter', () => {
responseFormatter.onRejected(error).catch(e =>
expect(e).toMatchInlineSnapshot(`
Object {
"correlation_id": "00000000-0000-0000-0000-000000000000",
"error_message": "Putio API Error",
"error_type": "API_ERROR",
"status_code": 400,
"x-trace-id": "MOCK_TRACE_ID",
}
`),
)
Expand All @@ -81,10 +81,10 @@ describe('interceptors/response/responseFormatter', () => {
responseFormatter.onRejected(error).catch(e =>
expect(e).toMatchInlineSnapshot(`
Object {
"correlation_id": undefined,
"error_message": "AXIOS_ERROR_MESSAGE",
"error_type": "ERROR",
"status_code": 502,
"x-trace-id": undefined,
}
`),
)
Expand All @@ -94,10 +94,10 @@ describe('interceptors/response/responseFormatter', () => {
responseFormatter.onRejected(mockPutioAPIClientError).catch(e =>
expect(e).toMatchInlineSnapshot(`
Object {
"correlation_id": undefined,
"error_message": "AXIOS_ERROR_MESSAGE",
"error_type": "ERROR",
"status_code": 0,
"x-trace-id": undefined,
}
`),
)
Expand Down Expand Up @@ -134,11 +134,11 @@ describe('interceptors/response/responseFormatter', () => {
responseFormatter.onRejected(error).catch(e =>
expect(e).toMatchInlineSnapshot(`
Object {
"correlation_id": undefined,
"error_message": "AXIOS_ERROR_MESSAGE",
"error_type": "ERROR",
"foo": "bar",
"status_code": 400,
"x-trace-id": undefined,
}
`),
)
Expand Down
3 changes: 1 addition & 2 deletions src/interceptors/response/responseFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
IPutioAPIClientErrorData,
PutioAPIClientResponseInterceptorFactory,
} from '../../client/types'
import { CORRELATION_ID_HEADER_NAME } from '../../constants'
import { isPutioAPIErrorResponse } from '../../utils'

export const createResponseFormatter: PutioAPIClientResponseInterceptorFactory = () => ({
Expand All @@ -19,7 +18,7 @@ export const createResponseFormatter: PutioAPIClientResponseInterceptorFactory =
}

let errorData: IPutioAPIClientErrorData = {
correlation_id: error.config.headers?.[CORRELATION_ID_HEADER_NAME],
'x-trace-id': error.response?.headers['x-trace-id'],
error_message: error.message,
error_type: 'ERROR',
status_code: 0,
Expand Down
20 changes: 20 additions & 0 deletions src/resources/Files/Files.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import PutioAPIClient, { isPutioAPIError } from '../../index'

describe('resources/Files/Files', () => {
const API = new PutioAPIClient({})

it('should construct correct payload for Upload method', async () => {
try {
API.setToken('test-token')

await API.Files.Upload({
file: new File([], 'test'),
parentId: 0,
})
} catch (e) {
if (isPutioAPIError(e)) {
expect(e.config.headers['Authorization']).toBe(`token test-token`)
}
}
})
})
15 changes: 11 additions & 4 deletions src/resources/Files/Files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,22 @@ export default class Files {
fileName?: string
parentId?: number
}) {
const FormData = require('form-data')
const form = new FormData()
form.append('file', file)
form.append('filename', fileName)
form.append('parent_id', parentId)

if (fileName) {
form.append('filename', fileName)
}

if (parentId) {
form.append('parent_id', parentId.toString())
}

return this.client.post('/files/upload', {
data: form,
headers: form.getHeaders(),
headers: {
'Content-Type': 'multipart/form-data',
},
})
}
}
11 changes: 4 additions & 7 deletions src/test-utils/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { AxiosError, AxiosRequestConfig } from 'axios'
import { CORRELATION_ID_HEADER_NAME, NIL_CORRELATION_ID } from '../constants'
import {
IPutioAPIClientError,
IPutioAPIClientErrorData,
IPutioAPIClientResponse,
} from '../client/types'

const mockRequestConfig: AxiosRequestConfig = {
headers: {
[CORRELATION_ID_HEADER_NAME]: NIL_CORRELATION_ID,
},
}
const mockRequestConfig: AxiosRequestConfig = {}

export const mockPutioAPIClientResponse: IPutioAPIClientResponse<{
foo: string
}> = {
config: mockRequestConfig,
data: { foo: 'bar', status: 'OK' },
headers: {},
headers: {
'x-trace-id': 'MOCK_TRACE_ID',
},
status: 200,
statusText: 'ok',
}
Expand Down
16 changes: 1 addition & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ colorette@^1.2.2:
resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.2.tgz#cbcc79d5e99caea2dbf10eb3a26fd8b3e6acfa94"
integrity sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==

combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
combined-stream@^1.0.6, combined-stream@~1.0.6:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
Expand Down Expand Up @@ -3194,15 +3194,6 @@ forever-agent@~0.6.1:
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=

form-data@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682"
integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
Expand Down Expand Up @@ -6527,11 +6518,6 @@ uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

v8-compile-cache@^2.0.3:
version "2.1.1"
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.1.tgz#54bc3cdd43317bca91e35dcaf305b1a7237de745"
Expand Down

0 comments on commit 6c582a1

Please sign in to comment.