芝麻web文件管理V1.00
编辑当前文件:/home/freeclou/optimyar/wp-content/plugins/code-snippets/js/utils/snippets/api.ts
import { addQueryArgs } from '@wordpress/url' import { REST_SNIPPETS_BASE } from '../restAPI' import { createSnippetObject } from './snippets' import type { RestAPI } from '../../hooks/useRestAPI' import type { SnippetSchema, WritableSnippetSchema } from '../../types/schema/SnippetSchema' import type { Snippet } from '../../types/Snippet' import type { SnippetsExport } from '../../types/schema/SnippetsExport' export interface SnippetsAPI { fetchAll: (network?: boolean | null) => Promise
fetch: (snippetId: number, network?: boolean | null) => Promise
create: (snippet: Snippet) => Promise
update: (snippet: Pick
& Partial
) => Promise
delete: (snippet: Pick
) => Promise
activate: (snippet: Pick
) => Promise
deactivate: (snippet: Pick
) => Promise
export: (snippet: Pick
) => Promise
exportCode: (snippet: Pick
) => Promise
attach: (snippet: Pick
) => Promise
detach: (snippet: Pick
) => Promise
} const buildURL = ({ id, network }: Pick
, action?: string) => addQueryArgs( [REST_SNIPPETS_BASE, id, action].filter(Boolean).join('/'), { network: network ? true : undefined } ) const mapToSchema = ({ name, desc, code, tags, scope, priority, active, network, conditionId }: Partial
): WritableSnippetSchema => ({ name, desc, code, tags, scope, priority, active, network, condition_id: conditionId }) export const buildSnippetsAPI = ({ get, post, del, put }: RestAPI): SnippetsAPI => ({ fetchAll: network => get
(addQueryArgs(REST_SNIPPETS_BASE, { network })) .then(response => response.map(createSnippetObject)), fetch: (snippetId, network) => get
(addQueryArgs(`${REST_SNIPPETS_BASE}/${snippetId}`, { network })) .then(createSnippetObject), create: snippet => post
(REST_SNIPPETS_BASE, mapToSchema(snippet)) .then(createSnippetObject), update: snippet => post
(snippet.id ? buildURL(snippet) : REST_SNIPPETS_BASE, mapToSchema(snippet)) .then(createSnippetObject), delete: snippet => del(buildURL(snippet)), activate: snippet => post
(buildURL(snippet, 'activate')) .then(createSnippetObject), deactivate: snippet => post
(buildURL(snippet, 'deactivate')) .then(createSnippetObject), export: snippet => get
(buildURL(snippet, 'export')), exportCode: snippet => get
(buildURL(snippet, 'export-code')), attach: snippet => put(buildURL(snippet, 'attach'), { condition_id: snippet.conditionId }), detach: snippet => put(buildURL(snippet, 'detach')) })