-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnippets.ts
More file actions
77 lines (72 loc) · 2.38 KB
/
snippets.ts
File metadata and controls
77 lines (72 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
CreateSnippetParams,
CreateSnippetResponse,
CreateSnippetResponseSchema,
DeleteSnippetParams,
DeleteSnippetResponse,
DeleteSnippetResponseSchema,
GetSnippetParams,
GetSnippetResponse,
GetSnippetResponseSchema,
GetSnippetsResponse,
GetSnippetsResponseSchema,
UpdateSnippetParams,
UpdateSnippetResponse,
UpdateSnippetResponseSchema,
} from "../types/snippets.js";
import type { BaseIterableClient, Constructor } from "./base.js";
/**
* Snippets operations mixin
*/
export function Snippets<T extends Constructor<BaseIterableClient>>(Base: T) {
return class extends Base {
async getSnippets(opts?: {
signal?: AbortSignal;
}): Promise<GetSnippetsResponse> {
const response = await this.client.get(
"/api/snippets",
opts?.signal ? { signal: opts.signal } : {}
);
return this.validateResponse(response, GetSnippetsResponseSchema);
}
async createSnippet(
params: CreateSnippetParams,
opts?: { signal?: AbortSignal }
): Promise<CreateSnippetResponse> {
const response = await this.client.post("/api/snippets", params, opts);
return this.validateResponse(response, CreateSnippetResponseSchema);
}
async getSnippet(
params: GetSnippetParams,
opts?: { signal?: AbortSignal }
): Promise<GetSnippetResponse> {
const response = await this.client.get(
`/api/snippets/${encodeURIComponent(String(params.identifier))}`,
opts?.signal ? { signal: opts.signal } : {}
);
return this.validateResponse(response, GetSnippetResponseSchema);
}
async updateSnippet(
params: UpdateSnippetParams,
opts?: { signal?: AbortSignal }
): Promise<UpdateSnippetResponse> {
const { identifier, ...body } = params;
const response = await this.client.put(
`/api/snippets/${encodeURIComponent(String(identifier))}`,
body,
opts
);
return this.validateResponse(response, UpdateSnippetResponseSchema);
}
async deleteSnippet(
params: DeleteSnippetParams,
opts?: { signal?: AbortSignal }
): Promise<DeleteSnippetResponse> {
const response = await this.client.delete(
`/api/snippets/${encodeURIComponent(String(params.identifier))}`,
opts?.signal ? { signal: opts.signal } : {}
);
return this.validateResponse(response, DeleteSnippetResponseSchema);
}
};
}