stacks.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { resolve } from '$app/paths';
  2. import type { StackCR } from '$lib/types/custom-resources/stack';
  3. import type { Stack } from '$lib/types/entities';
  4. import { handleResponse } from './types';
  5. const list = (workspaceId: string, fetch = globalThis.fetch): Promise<Stack[]> =>
  6. fetch(
  7. resolve('/api/workspaces/[workspaceId]/[resource=resource]', {
  8. workspaceId,
  9. resource: 'stacks',
  10. }),
  11. ).then(handleResponse<Stack[]>);
  12. const get = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<Stack> =>
  13. fetch(
  14. resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
  15. workspaceId,
  16. resource: 'stacks',
  17. id,
  18. }),
  19. ).then(handleResponse<Stack>);
  20. const getStatus = (
  21. workspaceId: string,
  22. id: string,
  23. fetch = globalThis.fetch,
  24. ): Promise<StackCR['status']> =>
  25. fetch(
  26. resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/status', {
  27. workspaceId,
  28. resource: 'stacks',
  29. id,
  30. }),
  31. ).then(handleResponse<StackCR['status']>);
  32. const create = (workspaceId: string, stack: Stack, fetch = globalThis.fetch): Promise<Stack> =>
  33. fetch(
  34. resolve('/api/workspaces/[workspaceId]/[resource=resource]', {
  35. workspaceId,
  36. resource: 'stacks',
  37. }),
  38. {
  39. method: 'POST',
  40. headers: { 'Content-Type': 'application/json' },
  41. body: JSON.stringify(stack),
  42. },
  43. ).then(handleResponse<Stack>);
  44. const update = (workspaceId: string, stack: Stack, fetch = globalThis.fetch): Promise<Stack> =>
  45. fetch(
  46. resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
  47. workspaceId,
  48. resource: 'stacks',
  49. id: stack.id,
  50. }),
  51. {
  52. method: 'PUT',
  53. headers: { 'Content-Type': 'application/json' },
  54. body: JSON.stringify(stack),
  55. },
  56. ).then(handleResponse<Stack>);
  57. const remove = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<void> =>
  58. fetch(
  59. resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
  60. workspaceId,
  61. resource: 'stacks',
  62. id,
  63. }),
  64. {
  65. method: 'DELETE',
  66. },
  67. ).then(async (res) => {
  68. if (!res.ok) await handleResponse<void>(res);
  69. });
  70. export { create, get, getStatus, list, remove, update };