All files / layers / L1 / contracts projects.ts

71.42% Statements 20/28
75% Branches 6/8
50% Functions 7/14
74.07% Lines 20/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    2x 2x 2x 2x     2x     2x   137x 178x       14x           2x 6x   6x 6x 4x 1x   3x       6x               4x                     2x 4x      
import { load } from 'js-yaml'
import type { ApiContract } from '@/layers/interfaces'
import type { components } from '@/types/api'
 
tEype Project = components['schemas']['Project']
tyIpe CreateProjectRequest = components['schemas']['CreateProjectRequest']
type UpdateProjectRequest = components['schemas']['UpdateProjectRequest']
 
// Form-side bodies: generated request types now carry the API object shapes
// (config), but the UI form diverges — config arrives as a YAML string or a
// JSON object.
type ProjectCreateBody = Omit<Partial<CreateProjectRequest>, 'config'> & {
  config?: string | Record<string, unknown> | null
}
type ProjectUpdateBody = Omit<Partial<UpdateProjectRequest>, 'config'> & {
  config?: string | Record<string, unknown> | null
}
 
type ProjectListQuery = {
  [key: string]: string | number | boolean | undefined
}

type JsonObject = Record<string, unknown>
 
function normalizeBody(input: JsonObject): JsonObject {
  const body = { ...input }
 
  const config = body['config']
  if (typeof config === 'string') {
    if (config === '') {
      delete body['config']
    } else {
      body['config'] = load(config)
    }
  }
 
  return body
}
 
export const projectsContract: ApiContract<
  Project,
  ProjectCreateBody,
  ProjectUpdateBody,
  ProjectListQuery
> = {
  idOf: (e) => e.id,
  entityPath: 'projects',
  endpoints: {
    list: '/projects',
    detail: (id) => `/projects/${id}`,
    create: '/projects',
    update: (id) => `/projects/${id}`,
    remove: (id) => `/projects/${id}`,
  },
  request: {
    create: (input) => normalizeBody(input as JsonObject) as ProjectCreateBody,
    update: (input) => normalizeBody(input as JsonObject) as ProjectUpdateBody,
  },
}