Skip to content

Commit

Permalink
Merge pull request #1 from superAlibi/dev
Browse files Browse the repository at this point in the history
merge dev
  • Loading branch information
superAlibi authored Jul 4, 2024
2 parents cb8b94e + 550606f commit 9987566
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ jobs:
id-token: write # The OIDC ID token is used for authentication with JSR.
steps:
- uses: actions/checkout@v4
- run: npx jsr publish
- name: Publish package
run: npx jsr publish
9 changes: 6 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "@advanced/fecher",
"version": "0.0.4",
"exports": "./mod.ts"
"name": "@advanced/fetcher",
"version": "0.0.5",
"exports": "./mod.ts",
"imports": {
"@cross/deepmerge": "jsr:@cross/deepmerge@^1.0.0"
}
}
19 changes: 19 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 44 additions & 26 deletions fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SyncEventDispatcher } from "./sync-event-dispatcher.ts";
import { deepMerge } from '@cross/deepmerge'
type EventType = "beforeRequest" | "afterResponse";
class InterceptorEvent extends Event {
public config?: FetherConfig;
Expand All @@ -14,50 +15,49 @@ class InterceptorEvent extends Event {
export type RequestInterceptor = (
req: InterceptorConfig,
) => Omit<InterceptorConfig, "data"> | Promise<Omit<InterceptorConfig, "data">>;

export type ResponseInterceptor<T = unknown> = (
resp: Response,
) => T | Promise<T>;
export interface FetherConfig extends RequestInit {
params?: string[][] | Record<string, string> | string | URLSearchParams;
params?: ConstructorParameters<typeof URLSearchParams>[number];
data?: unknown;
requestInterceptor?: RequestInterceptor;
responseInterceptor?: ResponseInterceptor;
}
export interface InterceptorConfig extends FetherConfig {
url: string;
}

/**
* 全局默认配置
* 仅仅配置了content-type=application/json
*/
export const defaultConfig: FetherConfig = {
headers: {
"Content-Type": "application/json",
},
};
export class Fetcher extends SyncEventDispatcher<{
beforeRequest: InterceptorEvent;
afterResponse: InterceptorEvent;
}> {
public baseURL: string;
constructor(
baseURL: string,
public config: FetherConfig = {
headers: {
"Content-Type": "application/json",
},
},
public baseURL: string,
public config: FetherConfig,
) {
super();
if (baseURL.endsWith("/")) {
this.baseURL = baseURL.slice(0);
} else {
this.baseURL = baseURL + "/";
}
}
static concatURL(baseURL: string, url: string): string {
static concatURL(baseURL: string, pathname: string): string {
if (!baseURL.endsWith("/")) {
baseURL += "/";
}
if (url.startsWith("/")) {
return baseURL + url.slice(1);
if (pathname.startsWith("/")) {
return baseURL + pathname.slice(1);
}
return baseURL + url;
return baseURL + pathname;
}
private getFormatedURL(url: string) {
return Fetcher.concatURL(this.baseURL, url);
private getFormatedURL(pathname: string) {
return Fetcher.concatURL(this.baseURL, pathname);
}
/**
* 如果options中存在data,那么会忽略body
Expand All @@ -67,10 +67,11 @@ export class Fetcher extends SyncEventDispatcher<{
*/
public async request<T = unknown>(
url: string,
options: Omit<FetherConfig, "url"> = {},
options: FetherConfig = {
},
): Promise<T> {
let { params, data, body, requestInterceptor, responseInterceptor } =
options;
let { params, data, body, requestInterceptor, responseInterceptor } = deepMerge(defaultConfig, options);


let req: Request;
if (requestInterceptor) {
Expand Down Expand Up @@ -136,19 +137,31 @@ export class Fetcher extends SyncEventDispatcher<{
return Promise.reject(resp);
});
}
/**
* 发起 http methods=get的请求
* @param url 相对baseURL的地址
* @param options 请求选项
* @returns
*/
public get<T = unknown>(
url: string,
options: Omit<FetherConfig, "url" | "body" | "data"> = {},
options: Omit<FetherConfig, "body" | "data"> = {},
): Promise<T> {
return this.request<T>(url, {
...this.config,
...options,
method: "GET",
});
}
/**
* 发起http method=post的请求
* @param url 相对baseURL的pathname
* @param options 其他请求选项
* @returns
*/
public post<T = unknown>(
url: string,
options: Omit<FetherConfig, "url"> = {},
options: FetherConfig = {},
): Promise<T> {
return this.request<T>(url, {
...this.config,
Expand All @@ -172,7 +185,7 @@ export class Fetcher extends SyncEventDispatcher<{
*/
public options<T = unknown>(
url: string,
options: Omit<FetherConfig, "url"> = {},
options: FetherConfig = {},
): Promise<T> {
return this.request<T>(url, {
...this.config,
Expand All @@ -181,6 +194,11 @@ export class Fetcher extends SyncEventDispatcher<{
});
}
}
/**
* 辅助创建Fetcher工具函数
* @param option
* @returns
*/
export const createFetcher = (
option: FetherConfig & { baseURL?: string },
): Fetcher => {
Expand Down

0 comments on commit 9987566

Please sign in to comment.