| 1 |
import { addQueryArgs } from '@wordpress/url' |
| 2 |
import { REST_SNIPPETS_BASE } from '../restAPI' |
| 3 |
import { createSnippetObject } from './snippets' |
| 4 |
import type { RestAPI } from '../../hooks/useRestAPI' |
| 5 |
import type { SnippetSchema, WritableSnippetSchema } from '../../types/schema/SnippetSchema' |
| 6 |
import type { Snippet } from '../../types/Snippet' |
| 7 |
import type { SnippetsExport } from '../../types/schema/SnippetsExport' |
| 8 |
|
| 9 |
export interface SnippetsAPI { |
| 10 |
fetchAll: (network?: boolean | null) => Promise<Snippet[]> |
| 11 |
fetch: (snippetId: number, network?: boolean | null) => Promise<Snippet> |
| 12 |
create: (snippet: Snippet) => Promise<Snippet> |
| 13 |
update: (snippet: Pick<Snippet, 'id' | 'network'> & Partial<Snippet>) => Promise<Snippet> |
| 14 |
delete: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<void> |
| 15 |
activate: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<Snippet> |
| 16 |
deactivate: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<Snippet> |
| 17 |
export: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<SnippetsExport> |
| 18 |
exportCode: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<string> |
| 19 |
attach: (snippet: Pick<Snippet, 'id' | 'network' | 'conditionId'>) => Promise<void> |
| 20 |
detach: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<void> |
| 21 |
} |
| 22 |
|
| 23 |
const buildURL = ({ id, network }: Pick<Snippet, 'id' | 'network'>, action?: string) => |
| 24 |
addQueryArgs( |
| 25 |
[REST_SNIPPETS_BASE, id, action].filter(Boolean).join('/'), |
| 26 |
{ network: network ? true : undefined } |
| 27 |
) |
| 28 |
|
| 29 |
const mapToSchema = ({ |
| 30 |
name, |
| 31 |
desc, |
| 32 |
code, |
| 33 |
tags, |
| 34 |
scope, |
| 35 |
priority, |
| 36 |
active, |
| 37 |
network, |
| 38 |
conditionId |
| 39 |
}: Partial<Snippet>): WritableSnippetSchema => ({ |
| 40 |
name, |
| 41 |
desc, |
| 42 |
code, |
| 43 |
tags, |
| 44 |
scope, |
| 45 |
priority, |
| 46 |
active, |
| 47 |
network, |
| 48 |
condition_id: conditionId |
| 49 |
}) |
| 50 |
|
| 51 |
export const buildSnippetsAPI = ({ get, post, del, put }: RestAPI): SnippetsAPI => ({ |
| 52 |
fetchAll: network => |
| 53 |
get<SnippetSchema[]>(addQueryArgs(REST_SNIPPETS_BASE, { network })) |
| 54 |
.then(response => response.map(createSnippetObject)), |
| 55 |
|
| 56 |
fetch: (snippetId, network) => |
| 57 |
get<SnippetSchema>(addQueryArgs(`${REST_SNIPPETS_BASE}/${snippetId}`, { network })) |
| 58 |
.then(createSnippetObject), |
| 59 |
|
| 60 |
create: snippet => |
| 61 |
post<SnippetSchema>(REST_SNIPPETS_BASE, mapToSchema(snippet)) |
| 62 |
.then(createSnippetObject), |
| 63 |
|
| 64 |
update: snippet => |
| 65 |
post<SnippetSchema>(snippet.id ? buildURL(snippet) : REST_SNIPPETS_BASE, mapToSchema(snippet)) |
| 66 |
.then(createSnippetObject), |
| 67 |
|
| 68 |
delete: snippet => |
| 69 |
del(buildURL(snippet)), |
| 70 |
|
| 71 |
activate: snippet => |
| 72 |
post<SnippetSchema>(buildURL(snippet, 'activate')) |
| 73 |
.then(createSnippetObject), |
| 74 |
|
| 75 |
deactivate: snippet => |
| 76 |
post<SnippetSchema>(buildURL(snippet, 'deactivate')) |
| 77 |
.then(createSnippetObject), |
| 78 |
|
| 79 |
export: snippet => |
| 80 |
get<SnippetsExport>(buildURL(snippet, 'export')), |
| 81 |
|
| 82 |
exportCode: snippet => |
| 83 |
get<string>(buildURL(snippet, 'export-code')), |
| 84 |
|
| 85 |
attach: snippet => |
| 86 |
put(buildURL(snippet, 'attach'), { condition_id: snippet.conditionId }), |
| 87 |
|
| 88 |
detach: snippet => |
| 89 |
put(buildURL(snippet, 'detach')) |
| 90 |
}) |
| 91 |
|