Skip to content

Commit

Permalink
refactor(admin/app/context): 将与 api 相关的功能添加至 api 属性之下
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Sep 10, 2024
1 parent abc558e commit aa2657e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 53 deletions.
88 changes: 47 additions & 41 deletions admin/src/app/context/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,53 +53,59 @@ export function buildContext(opt: Required<buildOptions>, f: API) {
const ctx = {
isLogin() { return f.isLogin(); },

/**
* 缓存 GET path 指向的数据
*
* @param path 相对于 baseURL 的接口地址;
* @param deps 缓存的依赖接口,这些依赖项的非 GET 接口一量被调用,将更新当前的缓存项;
*/
async cacheAPI(path: string, ...deps: Array<string>): Promise<void> { await f.cache(path, ...deps); },

/**
* 取消 GET path 指向的缓存数据
*/
async uncacheAPI(path: string): Promise<void> { await f.uncache(path); },

async clearCache(): Promise<void> { await f.clearCache(); },

async delete<R = never, PE = never>(path: string, withToken = true) {
return f.delete<R, PE>(path, withToken);
get api() {
return {
/**
* 缓存 GET path 指向的数据
*
* @param path 相对于 baseURL 的接口地址;
* @param deps 缓存的依赖接口,这些依赖项的非 GET 接口一量被调用,将更新当前的缓存项;
*/
async cache(path: string, ...deps: Array<string>): Promise<void> { await f.cache(path, ...deps); },

/**
* 取消 GET path 指向的缓存数据
*/
async uncache(path: string): Promise<void> { await f.uncache(path); },

async delete<R = never, PE = never>(path: string, withToken = true) {
return f.delete<R, PE>(path, withToken);
},

async put<R = never, PE = never>(path: string, body?: unknown, withToken = true) {
return f.put<R, PE>(path, body, withToken);
},

async patch<R = never, PE = never>(path: string, body?: unknown, withToken = true) {
return f.patch<R, PE>(path, body, withToken);
},

async post<R = never, PE = never>(path: string, body?: unknown, withToken = true) {
return f.post<R, PE>(path, body, withToken);
},

async get<R = never, PE = never>(path: string, withToken = true) { return f.get<R, PE>(path, withToken); },

async upload<R = never, PE = never>(path: string, obj: FormData, withToken = true) {
return f.upload<R, PE>(path, obj, withToken);
},

async request<R = never, PE = never>(path: string, method: Method, obj?: unknown, withToken = true) {
return f.request<R, PE>(path, method, obj, withToken);
},

async fetchWithArgument<R = never, PE = never>(path: string, method: Method, token?: string, ct?: string, body?: BodyInit) {
return f.withArgument<R, PE>(path, method, token, ct, body);
},

async fetch<R = never, PE = never>(path: string, req?: RequestInit) { return f.fetch<R, PE>(path, req); },
};
},

async put<R = never, PE = never>(path: string, body?: unknown, withToken = true) {
return f.put<R,PE>(path, body, withToken);
},

async patch<R = never, PE = never>(path: string, body?: unknown, withToken = true) {
return f.patch<R, PE>(path, body, withToken);
},

async post<R = never, PE = never>(path: string, body?: unknown, withToken = true) {
return f.post<R, PE>(path, body, withToken);
},

async get<R = never, PE = never>(path: string, withToken = true) { return f.get<R, PE>(path, withToken); },

async upload<R = never, PE = never>(path: string, obj: FormData, withToken = true) {
return f.upload<R, PE>(path, obj, withToken);
},

async request<R = never, PE = never>(path: string, method: Method, obj?: unknown, withToken = true) {
return f.request<R, PE>(path, method, obj, withToken);
},

async fetchWithArgument<R = never, PE = never>(path: string, method: Method, token?: string, ct?: string, body?: BodyInit) {
return f.withArgument<R, PE>(path, method, token, ct, body);
},

async fetch<R = never, PE = never>(path: string, req?: RequestInit) { return f.fetch<R, PE>(path, req); },

/**
* 将 {@link Problem} 作为错误进行处理,用户可以自行处理部分常用的错误,剩余的交由此方法处理。
*
Expand Down
2 changes: 1 addition & 1 deletion admin/src/components/form/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export class FormAccessor<T extends object, R = never, P = never> {
const obj = this.#object.object(this.#validation);
if (!obj) { return false; }

const ret = await this.#ctx.request<R, P>(this.#path, this.#method, obj, this.#withToken);
const ret = await this.#ctx.api.request<R, P>(this.#path, this.#method, obj, this.#withToken);
if (ret.ok) {
if (this.#success) {
this.#success(ret);
Expand Down
6 changes: 3 additions & 3 deletions admin/src/components/table/remote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default function<T extends object, Q extends Query>(props: Props<T,Q>) {
async refresh(): Promise<void> { await ref.refresh(); },

async delete<T extends string|number>(id: T): Promise<void> {
const ret = await ctx.delete(`${props.path}/${id}`);
const ret = await ctx.api.delete(`${props.path}/${id}`);
if (!ret.ok) {
await ctx.outputProblem(ret.status, ret.body);
return;
Expand All @@ -74,7 +74,7 @@ export default function<T extends object, Q extends Query>(props: Props<T,Q>) {

function buildPagingLoadFunc<T extends object, Q extends Query>(ctx: AppContext, path: string) {
return async (q: Q): Promise<Page<T> | undefined> => {
const ret = await ctx.get<Page<T>>(path + query2Search(q));
const ret = await ctx.api.get<Page<T>>(path + query2Search(q));
if (!ret.ok) {
ctx.outputProblem(ret.status, ret.body);
return { count: 0, current: [] };
Expand All @@ -85,7 +85,7 @@ function buildPagingLoadFunc<T extends object, Q extends Query>(ctx: AppContext,

function buildNoPagingLoadFunc<T extends object, Q extends Query>(ctx: AppContext, path: string) {
return async (q: Q): Promise<Array<T> | undefined> => {
const ret = await ctx.get<Array<T>>(path + query2Search(q));
const ret = await ctx.api.get<Array<T>>(path + query2Search(q));
if (!ret.ok) {
ctx.outputProblem(ret.status, ret.body);
return [];
Expand Down
6 changes: 3 additions & 3 deletions admin/src/pages/roles/permission.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function() {
const [resources, setResources] = createSignal<Array<Resource>>([], {equals: false});

const [roleResource] = createResource(async () => {
const ret = await ctx.get<RoleResource>(`/roles/${ps.id}/resources`);
const ret = await ctx.api.get<RoleResource>(`/roles/${ps.id}/resources`);
if (!ret.ok) {
await ctx.outputProblem(ret.status, ret.body);
return;
Expand All @@ -42,7 +42,7 @@ export default function() {
setParent(b ? (b.parent ?? []) : []);
setCurrent(b ? (b.current ?? []) : []);

const ret = await ctx.get<Array<Resource>>('/resources');
const ret = await ctx.api.get<Array<Resource>>('/resources');
if (!ret.ok) {
await ctx.outputProblem(ret.status, ret.body);
return;
Expand All @@ -52,7 +52,7 @@ export default function() {
});

const save = async()=>{
const ret = await ctx.put(`/roles/${ps.id}/resources`, current());
const ret = await ctx.api.put(`/roles/${ps.id}/resources`, current());
if (!ret.ok) {
await ctx.outputProblem(ret.status, ret.body);
return;
Expand Down
6 changes: 3 additions & 3 deletions admin/src/pages/roles/roles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function Roles(props: Props): JSX.Element {
const current = new ObjectAccessor({} as Role);
const currentID = current.accessor('id');

ctx.cacheAPI('/roles','/roles/*');
ctx.api.cache('/roles','/roles/*');

// 保存数据
const save = async (): Promise<undefined> => {
Expand All @@ -42,9 +42,9 @@ export default function Roles(props: Props): JSX.Element {
const obj = current.object();
delete obj.id;
if (id) {
ret = await ctx.put(`/roles/${id}`, obj);
ret = await ctx.api.put(`/roles/${id}`, obj);
} else {
ret = await ctx.post('/roles', obj);
ret = await ctx.api.post('/roles', obj);
}

if (!ret.ok) {
Expand Down
2 changes: 1 addition & 1 deletion admin/src/pages/system/info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function(): JSX.Element {
const ctx = useApp();

const [info] = createResource(async()=>{
const ret = await ctx.get<Info>('/system/info');
const ret = await ctx.api.get<Info>('/system/info');
if (!ret.ok) {
await ctx.outputProblem(ret.status, ret.body);
return;
Expand Down
2 changes: 1 addition & 1 deletion admin/src/pages/system/services.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function(): JSX.Element {
const ctx = useApp();

const items = createMemo(async()=>{
const ret = await ctx.get<Service>('/system/services');
const ret = await ctx.api.get<Service>('/system/services');
if (!ret.ok) {
await ctx.outputProblem(ret.status, ret.body);
return;
Expand Down

0 comments on commit aa2657e

Please sign in to comment.