Skip to content

Commit

Permalink
Remove dom types
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Mar 21, 2023
1 parent 717f7b4 commit bf32773
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/request/feature/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClientRequest } from "http";
import { Feature, Options, Init } from "..";
import { Feature, Options, Init, FetchOptions } from "..";

declare module ".." {
interface Init {
Expand All @@ -23,7 +23,7 @@ export class DataFeature implements Feature {
}
}

forFetch(init: Init, options: RequestInit) {
forFetch(init: Init, options: FetchOptions) {
if (init.data) {
options.headers = {
...options.headers,
Expand Down
4 changes: 2 additions & 2 deletions src/request/feature/form.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClientRequest } from "http";
import { Feature, Options, Init, StreamFile } from "..";
import { Feature, Options, Init, StreamFile, FetchOptions } from "..";
import { ReadStream } from "fs";

declare module ".." {
Expand Down Expand Up @@ -51,7 +51,7 @@ export class FormFeature implements Feature {
}
}

forFetch(init: Init, options: RequestInit) {
forFetch(init: Init, options: FetchOptions) {
if (init.form) {
options.headers = {
...options.headers,
Expand Down
4 changes: 2 additions & 2 deletions src/request/feature/headers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClientRequest } from "http";
import { Feature, Options, Init } from "..";
import { Feature, Options, Init, FetchOptions } from "..";

declare module ".." {
interface Init {
Expand All @@ -19,7 +19,7 @@ export class HeadersFeature implements Feature {

async forRequestClient(init: Init, client: ClientRequest) {}

forFetch(init: Init, options: RequestInit) {
forFetch(init: Init, options: FetchOptions) {
if (init.headers) {
options.headers = {
...options.headers,
Expand Down
4 changes: 2 additions & 2 deletions src/request/feature/method.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClientRequest } from "http";
import { Feature, Options, Init } from "..";
import { Feature, Options, Init, FetchOptions } from "..";

declare module ".." {
interface Init {
Expand All @@ -14,7 +14,7 @@ export class MethodFeature implements Feature {

async forRequestClient(init: Init, client: ClientRequest) {}

forFetch(init: Init, options: RequestInit) {
forFetch(init: Init, options: FetchOptions) {
options.method = init.method || "GET";
}
}
4 changes: 2 additions & 2 deletions src/request/feature/signal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClientRequest } from "http";
import { Feature, Options, Init } from "..";
import { Feature, Options, Init, FetchOptions } from "..";

declare module ".." {
interface Init {
Expand All @@ -16,7 +16,7 @@ export class SignalFeature implements Feature {

async forRequestClient(init: Init, client: ClientRequest) {}

forFetch(init: Init, options: RequestInit) {
forFetch(init: Init, options: FetchOptions) {
if (init.signal) {
options.signal = init.signal;
}
Expand Down
19 changes: 14 additions & 5 deletions src/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ export type Format = 'json' | 'text' | 'original';
export interface Feature {
forRequest: (init: Init, options: Options) => void;
forRequestClient: (init: Init, client: ClientRequest) => Promise<void>;
forFetch: (init: Init, options: RequestInit) => void;
forFetch: (init: Init, options: FetchOptions) => void;
}

export type FetchOptions = {
method?: string;
headers?: Record<string, string>;
body?: any;
signal?: AbortSignal;
};

export type Response = {
status: number;
statusText: string;
Expand All @@ -28,7 +35,7 @@ export class StreamFile {

public stream: Blob | ReadStream;

constructor(file: string | File) {
constructor(file: string | Blob) {
if (typeof file === "string") {
const { createReadStream } = require("fs");
this.stream = createReadStream(file);
Expand All @@ -44,7 +51,7 @@ export class StreamFile {
}
}

export function readFile(file: string | File) {
export function readFile(file: string | Blob) {
return new StreamFile(file);
}

Expand All @@ -57,13 +64,15 @@ export async function request(url: string, init: Init, format: Format): Promise<
};

if (typeof fetch === "function") {
const options: RequestInit = {};
const options: FetchOptions = {};
await requestFeature(async (feature) => feature.forFetch(init, options));

const resp = await fetch(url, options);
response.status = resp.status;
response.statusText = resp.statusText;
response.headers = Object.fromEntries(resp.headers.entries());
resp.headers.forEach((value, key) => {
response.headers[key.toLocaleLowerCase()] = value;
});

if (format === "json") {
if (!resp.headers.get("content-type")?.match(/^application\/json/)) {
Expand Down
4 changes: 2 additions & 2 deletions src/v1/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Audio = Partial<{
}>;

export function createAudioTranscription(client: ApiClient) {
return async (request: CreateAudioTranscriptionRequest, file: string | File): Promise<Audio> => {
return async (request: CreateAudioTranscriptionRequest, file: string | Blob): Promise<Audio> => {
const form: Record<string, any> = {};

for (const key in request) {
Expand All @@ -36,7 +36,7 @@ export function createAudioTranscription(client: ApiClient) {
}

export function createAudioTranslation(client: ApiClient) {
return async (request: CreateAudioTranslationRequest, file: string | File): Promise<Audio> => {
return async (request: CreateAudioTranslationRequest, file: string | Blob): Promise<Audio> => {
const form: Record<string, any> = {};

for (const key in request) {
Expand Down
2 changes: 1 addition & 1 deletion src/v1/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function listFiles(client: ApiClient) {
}

export function uploadFile(client: ApiClient) {
return async (file: string | File, purpose: string): Promise<FileObject> => {
return async (file: string | Blob, purpose: string): Promise<FileObject> => {
const form: Record<string, any> = {
'purpose': purpose,
'file': readFile(file)
Expand Down
4 changes: 2 additions & 2 deletions src/v1/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function createImage(client: ApiClient) {
}

export function editImage(client: ApiClient) {
return async (request: EditImageRequest, image: string | File, mask?: string | File): Promise<Image> => {
return async (request: EditImageRequest, image: string | Blob, mask?: string | Blob): Promise<Image> => {
const form: Record<string, any> = {};

for (const key in request) {
Expand All @@ -62,7 +62,7 @@ export function editImage(client: ApiClient) {
}

export function createImageVariation(client: ApiClient) {
return async (request: CreateImageVariationRequest, image: string | File): Promise<Image> => {
return async (request: CreateImageVariationRequest, image: string | Blob): Promise<Image> => {
const form: Record<string, any> = {};

for (const key in request) {
Expand Down

0 comments on commit bf32773

Please sign in to comment.