-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed usage of lodash from ArrayBuffer and added AxiosJsonUncompressed
- Loading branch information
Showing
12 changed files
with
260 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,7 @@ | |
"typeeof", | ||
"userid", | ||
"webp", | ||
"Wermke", | ||
"wordlist", | ||
"xdescribe", | ||
"xtest", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './equalArrayBuffers.ts' | ||
export * from './isArrayBuffer.ts' | ||
export * from './toArrayBuffer.ts' | ||
export * from './toUint8Array.ts' | ||
export { isArrayBuffer } from '@xylabs/lodash' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { | ||
describe, expect, test, | ||
} from 'vitest' | ||
|
||
import { isArrayBuffer } from './isArrayBuffer.ts' | ||
|
||
describe('isArrayBuffer', () => { | ||
test('actual ArrayBuffer', () => { | ||
expect(isArrayBuffer(new ArrayBuffer(0))).toBe(true) | ||
}) | ||
test('UInt8Array.buffer', () => { | ||
expect(isArrayBuffer(new Uint8Array(0).buffer)).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const isArrayBuffer = (value: unknown): value is ArrayBuffer => { | ||
return value instanceof ArrayBuffer | ||
} | ||
|
||
export const isArrayBufferLike = (value: unknown): value is ArrayBufferLike => { | ||
return ( | ||
value !== null | ||
&& typeof value === 'object' | ||
&& 'byteLength' in value | ||
&& typeof (value as ArrayBufferLike).byteLength === 'number' | ||
&& 'slice' in value | ||
&& typeof (value as ArrayBufferLike).slice === 'function' | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { Logger } from '@xylabs/logger' | ||
import type { AxiosResponse, RawAxiosRequestConfig } from 'axios' | ||
import { Axios, AxiosHeaders } from 'axios' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type RawAxiosJsonRequestUncompressedConfig<D = any> = RawAxiosRequestConfig<D> | ||
|
||
export class AxiosJsonUncompressed extends Axios { | ||
static defaultLogger?: Logger | ||
|
||
constructor(config?: RawAxiosJsonRequestUncompressedConfig) { | ||
super(AxiosJsonUncompressed.axiosConfig(config)) | ||
} | ||
|
||
static finalPath(response: AxiosResponse) { | ||
if (response.request.path) { | ||
// nodejs | ||
return response.request.path.split('/').pop() | ||
} else if (response.request.responseURL) { | ||
// browser | ||
return response.request.responseURL.split('/').pop() | ||
} else { | ||
this.defaultLogger?.warn('Failed to get final path from response') | ||
} | ||
} | ||
|
||
private static axiosConfig({ headers, ...config }: RawAxiosJsonRequestUncompressedConfig = {}): RawAxiosJsonRequestUncompressedConfig { | ||
return { | ||
headers: this.buildHeaders(headers), | ||
transformRequest: (data) => { | ||
const json = JSON.stringify(data) | ||
return JSON.stringify(json) | ||
}, | ||
transformResponse: (data) => { | ||
try { | ||
return JSON.parse(data) | ||
} catch { | ||
return null | ||
} | ||
}, | ||
...config, | ||
} | ||
} | ||
|
||
private static buildHeaders(headers: RawAxiosJsonRequestUncompressedConfig['headers']) { | ||
const axiosHeaders = new AxiosHeaders() | ||
axiosHeaders.set('Accept', 'application/json, text/plain, *.*') | ||
axiosHeaders.set('Content-Type', 'application/json') | ||
for (const [key, value] of Object.entries(headers ?? {})) axiosHeaders.set(key, value) | ||
return axiosHeaders | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import { AxiosJson } from './AxiosJson.ts' | ||
import { AxiosJsonUncompressed } from './AxiosJsonUncompressed.ts' | ||
|
||
export * from './AxiosJson.ts' | ||
export * from './AxiosJsonUncompressed.ts' | ||
export { gzip } from 'pako' | ||
|
||
export const axios = new AxiosJson() | ||
export const axiosUncompressed = new AxiosJsonUncompressed() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
// eslint-disable-next-line no-restricted-imports | ||
export * from 'lodash-es' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// eslint-disable-next-line import-x/no-internal-modules | ||
import { defineWorkspace } from 'vitest/config' | ||
|
||
export default defineWorkspace([ | ||
'./vitest.config.ts', | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters