|
| 1 | +import { Ok, Err, Result } from 'ts-results-es'; |
| 2 | + |
| 3 | +import { extractErrorMessageOrDefault } from '@/lib/utils'; |
| 4 | + |
| 5 | +import { |
| 6 | + ApiCreateProjectResponse, |
| 7 | + ApiCreateWorkspaceResponse, |
| 8 | + ApiListAllProjectsResponse, |
| 9 | + ApiListAllWorkspacesResponse, |
| 10 | +} from './types'; |
| 11 | + |
| 12 | +const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT; |
| 13 | +if (!patternCoreEndpoint) { |
| 14 | + throw new Error('PATTERN_CORE_ENDPOINT is not set'); |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Get all workspaces |
| 19 | + * @param accessToken |
| 20 | + * @returns result containing all workspaces of current user |
| 21 | + */ |
| 22 | +export const getAllWorkspaces = async ( |
| 23 | + accessToken: string, |
| 24 | +): Promise<Result<ApiListAllWorkspacesResponse, string>> => { |
| 25 | + try { |
| 26 | + const allWorkspacesResponse = await fetch( |
| 27 | + `${patternCoreEndpoint}/workspace`, |
| 28 | + { |
| 29 | + headers: { |
| 30 | + Authorization: `Bearer ${accessToken}`, |
| 31 | + 'Content-Type': 'application/json', |
| 32 | + }, |
| 33 | + }, |
| 34 | + ); |
| 35 | + |
| 36 | + if (allWorkspacesResponse.ok) { |
| 37 | + const allWorkspaces: ApiListAllWorkspacesResponse = ( |
| 38 | + await allWorkspacesResponse.json() |
| 39 | + ).data; |
| 40 | + |
| 41 | + return Ok(allWorkspaces); |
| 42 | + } |
| 43 | + return Err( |
| 44 | + `Fetching workspaces failed with error code ${allWorkspacesResponse.status}`, |
| 45 | + ); |
| 46 | + } catch (error) { |
| 47 | + return Err(extractErrorMessageOrDefault(error)); |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * Create a workspace |
| 53 | + * @param accessToken |
| 54 | + * @returns result containing the created workspace |
| 55 | + */ |
| 56 | +export const createWorkspace = async ( |
| 57 | + accessToken: string, |
| 58 | +): Promise<Result<ApiCreateWorkspaceResponse, string>> => { |
| 59 | + try { |
| 60 | + const response = await fetch(`${patternCoreEndpoint}/workspace`, { |
| 61 | + method: 'POST', |
| 62 | + headers: { |
| 63 | + Authorization: `Bearer ${accessToken}`, |
| 64 | + 'Content-Type': 'application/json', |
| 65 | + }, |
| 66 | + body: JSON.stringify({ |
| 67 | + name: 'Default Workspace', |
| 68 | + }), |
| 69 | + }); |
| 70 | + |
| 71 | + if (response.ok) { |
| 72 | + const workspace: ApiCreateWorkspaceResponse = (await response.json()) |
| 73 | + .data; |
| 74 | + |
| 75 | + return Ok(workspace); |
| 76 | + } |
| 77 | + return Err(`Creating workspace failed with error code ${response.status}`); |
| 78 | + } catch (error) { |
| 79 | + return Err(extractErrorMessageOrDefault(error)); |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * Get all projects |
| 85 | + * @param accessToken |
| 86 | + * @returns result containing all projects of current user |
| 87 | + */ |
| 88 | +export const getAllProjects = async ( |
| 89 | + accessToken: string, |
| 90 | +): Promise<Result<ApiListAllProjectsResponse, string>> => { |
| 91 | + try { |
| 92 | + const allProjectsResponse = await fetch(`${patternCoreEndpoint}/project`, { |
| 93 | + headers: { |
| 94 | + Authorization: `Bearer ${accessToken}`, |
| 95 | + 'Content-Type': 'application/json', |
| 96 | + }, |
| 97 | + }); |
| 98 | + if (allProjectsResponse.ok) { |
| 99 | + const allProjects: ApiListAllProjectsResponse = ( |
| 100 | + await allProjectsResponse.json() |
| 101 | + ).data; |
| 102 | + |
| 103 | + return Ok(allProjects); |
| 104 | + } |
| 105 | + return Err( |
| 106 | + `Fetching projects failed with error code ${allProjectsResponse.status}`, |
| 107 | + ); |
| 108 | + } catch (error) { |
| 109 | + return Err(extractErrorMessageOrDefault(error)); |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +/** |
| 114 | + * Create a project in a workspace |
| 115 | + * @param accessToken |
| 116 | + * @param workspaceId |
| 117 | + * @returns result containing the created project |
| 118 | + */ |
| 119 | +export const createProjectInWorkspace = async ( |
| 120 | + accessToken: string, |
| 121 | + workspaceId: string, |
| 122 | +): Promise<Result<ApiCreateProjectResponse, string>> => { |
| 123 | + try { |
| 124 | + const response = await fetch(`${patternCoreEndpoint}/project`, { |
| 125 | + method: 'POST', |
| 126 | + headers: { |
| 127 | + Authorization: `Bearer ${accessToken}`, |
| 128 | + 'Content-Type': 'application/json', |
| 129 | + }, |
| 130 | + body: JSON.stringify({ |
| 131 | + name: 'Default Project', |
| 132 | + workspace_id: workspaceId, |
| 133 | + }), |
| 134 | + }); |
| 135 | + |
| 136 | + if (response.ok) { |
| 137 | + const project: ApiCreateProjectResponse = (await response.json()).data; |
| 138 | + |
| 139 | + return Ok(project); |
| 140 | + } |
| 141 | + return Err(`Creating project failed with error code ${response.status}`); |
| 142 | + } catch (error) { |
| 143 | + return Err(extractErrorMessageOrDefault(error)); |
| 144 | + } |
| 145 | +}; |
0 commit comments