Skip to content

Commit

Permalink
feat(http): support ctx.http.isError
Browse files Browse the repository at this point in the history
also fix file-type imports
  • Loading branch information
shigma committed May 11, 2024
1 parent 6ca21e9 commit f2d8908
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/adapter/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fileURLToPath } from 'node:url'
import { basename } from 'node:path'
import { fromBuffer } from 'file-type'
import FileType from 'file-type'
import { FileResponse } from '../index.js'
import { readFile } from 'node:fs/promises'

Expand All @@ -9,7 +9,7 @@ export { lookup } from 'node:dns/promises'
export async function loadFile(url: string): Promise<FileResponse | undefined> {
if (url.startsWith('file://')) {
const data = await readFile(fileURLToPath(url))
const result = await fromBuffer(data)
const result = await FileType.fromBuffer(data)
// https://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer#answer-31394257
const buffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength)
return { mime: result?.mime, filename: basename(url), data: buffer }
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export interface HTTP {

export class HTTP extends Service<HTTP.Config> {
static Error = HTTPError
/** @deprecated use `HTTP.Error.is()` instead */
/** @deprecated use `http.isError()` instead */
static isAxiosError = HTTPError.is

static [Service.provide] = 'http'
Expand All @@ -163,6 +163,8 @@ export class HTTP extends Service<HTTP.Config> {
}
}

public isError = HTTPError.is

private _decoders: Dict = Object.create(null)

constructor(config?: HTTP.Config)
Expand Down

0 comments on commit f2d8908

Please sign in to comment.