PluginProbe
Code Snippets / 3.7.0
Code Snippets v3.7.0
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / js / utils / snippets / api.ts

api.ts in Code Snippets 3.7.0, at js/utils/snippets/api.ts

91 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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