-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCreateStyleTool.ts
More file actions
46 lines (39 loc) · 1.1 KB
/
CreateStyleTool.ts
File metadata and controls
46 lines (39 loc) · 1.1 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
import { MapboxApiBasedTool } from '../MapboxApiBasedTool.js';
import {
CreateStyleSchema,
CreateStyleInput
} from './CreateStyleTool.schema.js';
export class CreateStyleTool extends MapboxApiBasedTool<
typeof CreateStyleSchema
> {
name = 'create_style_tool';
description = 'Create a new Mapbox style';
constructor() {
super({ inputSchema: CreateStyleSchema });
}
protected async execute(
input: CreateStyleInput,
accessToken?: string
): Promise<any> {
const username = MapboxApiBasedTool.getUserNameFromToken(accessToken);
const url = `${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}styles/v1/${username}?access_token=${accessToken}`;
const payload = {
name: input.name,
...input.style
};
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(
`Failed to create style: ${response.status} ${response.statusText}`
);
}
const data = await response.json();
return data;
}
}