(entityType: string)
| 57 | }; |
| 58 | |
| 59 | export function createStorageAPI<T extends StorageEntity>(entityType: string) { |
| 60 | return { |
| 61 | async get(name: string): Promise<T> { |
| 62 | const response = await api.fetch<T>(`/${entityType}/${name}`); |
| 63 | if (response.error) throw new Error(response.error); |
| 64 | return response.data as T; |
| 65 | }, |
| 66 | |
| 67 | async getNames(): Promise<string[]> { |
| 68 | const response = await api.fetch<string[]>(`/${entityType}/names`); |
| 69 | if (response.error) throw new Error(response.error); |
| 70 | return response.data as []; |
| 71 | }, |
| 72 | |
| 73 | async delete(name: string): Promise<void> { |
| 74 | const response = await api.fetch(`/${entityType}/${name}`, { method: 'DELETE' }); |
| 75 | if (response.error) throw new Error(response.error); |
| 76 | }, |
| 77 | |
| 78 | async exists(name: string): Promise<boolean> { |
| 79 | const response = await api.fetch<boolean>(`/${entityType}/exists/${name}`); |
| 80 | if (response.error) throw new Error(response.error); |
| 81 | return response.data as boolean; |
| 82 | }, |
| 83 | |
| 84 | async rename(oldName: string, newName: string): Promise<void> { |
| 85 | const response = await api.fetch(`/${entityType}/rename/${oldName}/${newName}`, { method: 'PUT' }); |
| 86 | if (response.error) throw new Error(response.error); |
| 87 | }, |
| 88 | |
| 89 | async save(name: string, content: string | object): Promise<void> { |
| 90 | const response = await api.fetch(`/${entityType}/${name}`, { |
| 91 | method: 'POST', |
| 92 | body: JSON.stringify(content), |
| 93 | headers: { 'Content-Type': 'application/json' }, |
| 94 | }); |
| 95 | if (response.error) throw new Error(response.error); |
| 96 | }, |
| 97 | }; |
| 98 | } |
no outgoing calls
no test coverage detected