Skip to content

Commit

Permalink
use Writeable instead of Mutable in type naming
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Jan 9, 2025
1 parent 089837f commit a69c1e6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/hooks/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
documentByVersionIdQueryOptions,
documentsQueryOptions,
updateDocumentMutationOptions,
type MutableDocumentType,
type WriteableDocumentType,
} from '../lib/react-query/documents.js'
import { useSingleProject } from './projects.js'

Expand Down Expand Up @@ -45,7 +45,7 @@ type ReadHookResult<D> = {
* }
* ```
*/
export function useSingleDocByDocId<D extends MutableDocumentType>({
export function useSingleDocByDocId<D extends WriteableDocumentType>({
projectId,
docType,
docId,
Expand Down Expand Up @@ -101,7 +101,7 @@ export function useSingleDocByDocId<D extends MutableDocumentType>({
* }
* ```
*/
export function useSingleDocByVersionId<D extends MutableDocumentType>({
export function useSingleDocByVersionId<D extends WriteableDocumentType>({
projectId,
docType,
versionId,
Expand Down Expand Up @@ -166,7 +166,7 @@ export function useSingleDocByVersionId<D extends MutableDocumentType>({
* }
* ```
*/
export function useManyDocs<D extends MutableDocumentType>({
export function useManyDocs<D extends WriteableDocumentType>({
projectId,
docType,
includeDeleted,
Expand Down Expand Up @@ -203,7 +203,7 @@ export function useManyDocs<D extends MutableDocumentType>({
* @param opts.docType Document type to create.
* @param opts.projectId Public ID of project to create document for.
*/
export function useCreateDocument<D extends MutableDocumentType>({
export function useCreateDocument<D extends WriteableDocumentType>({
docType,
projectId,
}: {
Expand Down Expand Up @@ -231,7 +231,7 @@ export function useCreateDocument<D extends MutableDocumentType>({
* @param opts.docType Document type to update.
* @param opts.projectId Public ID of project document belongs to.
*/
export function useUpdateDocument<D extends MutableDocumentType>({
export function useUpdateDocument<D extends WriteableDocumentType>({
docType,
projectId,
}: {
Expand Down Expand Up @@ -259,7 +259,7 @@ export function useUpdateDocument<D extends MutableDocumentType>({
* @param opts.docType Document type to delete.
* @param opts.projectId Public ID of project document belongs to.
*/
export function useDeleteDocument<D extends MutableDocumentType>({
export function useDeleteDocument<D extends WriteableDocumentType>({
docType,
projectId,
}: {
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export {
getDocumentByVersionIdQueryKey,
getDocumentsQueryKey,
getManyDocumentsQueryKey,
type MutableDocument,
type MutableDocumentType,
type MutableValue,
type WriteableDocument,
type WriteableDocumentType,
type WriteableValue,
} from './lib/react-query/documents.js'
export {
getInvitesQueryKey,
Expand Down
46 changes: 25 additions & 21 deletions src/lib/react-query/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ import {
ROOT_QUERY_KEY,
} from './shared.js'

export type MutableDocumentType = Extract<
export type WriteableDocumentType = Extract<
MapeoDoc['schemaName'],
'field' | 'observation' | 'preset' | 'track' | 'remoteDetectionAlert'
>
export type MutableValue<D extends MutableDocumentType> = Extract<
export type WriteableValue<D extends WriteableDocumentType> = Extract<
MapeoValue,
{ schemaName: D }
>
export type MutableDocument<D extends MutableDocumentType> = Extract<
export type WriteableDocument<D extends WriteableDocumentType> = Extract<
MapeoDoc,
{ schemaName: D }
>

export function getDocumentsQueryKey<D extends MutableDocumentType>({
export function getDocumentsQueryKey<D extends WriteableDocumentType>({
projectId,
docType,
}: {
Expand All @@ -38,7 +38,7 @@ export function getDocumentsQueryKey<D extends MutableDocumentType>({
return [ROOT_QUERY_KEY, 'projects', projectId, docType] as const
}

export function getManyDocumentsQueryKey<D extends MutableDocumentType>({
export function getManyDocumentsQueryKey<D extends WriteableDocumentType>({
projectId,
docType,
includeDeleted,
Expand All @@ -58,7 +58,7 @@ export function getManyDocumentsQueryKey<D extends MutableDocumentType>({
] as const
}

export function getDocumentByDocIdQueryKey<D extends MutableDocumentType>({
export function getDocumentByDocIdQueryKey<D extends WriteableDocumentType>({
projectId,
docType,
docId,
Expand All @@ -79,7 +79,9 @@ export function getDocumentByDocIdQueryKey<D extends MutableDocumentType>({
] as const
}

export function getDocumentByVersionIdQueryKey<D extends MutableDocumentType>({
export function getDocumentByVersionIdQueryKey<
D extends WriteableDocumentType,
>({
projectId,
docType,
versionId,
Expand All @@ -100,7 +102,7 @@ export function getDocumentByVersionIdQueryKey<D extends MutableDocumentType>({
] as const
}

export function documentsQueryOptions<D extends MutableDocumentType>({
export function documentsQueryOptions<D extends WriteableDocumentType>({
projectApi,
projectId,
docType,
Expand Down Expand Up @@ -131,7 +133,7 @@ export function documentsQueryOptions<D extends MutableDocumentType>({
}

export function documentByDocumentIdQueryOptions<
D extends MutableDocumentType,
D extends WriteableDocumentType,
>({
projectApi,
projectId,
Expand Down Expand Up @@ -163,7 +165,9 @@ export function documentByDocumentIdQueryOptions<
})
}

export function documentByVersionIdQueryOptions<D extends MutableDocumentType>({
export function documentByVersionIdQueryOptions<
D extends WriteableDocumentType,
>({
projectApi,
projectId,
docType,
Expand All @@ -190,7 +194,7 @@ export function documentByVersionIdQueryOptions<D extends MutableDocumentType>({
})
}

export function createDocumentMutationOptions<D extends MutableDocumentType>({
export function createDocumentMutationOptions<D extends WriteableDocumentType>({
docType,
projectApi,
projectId,
Expand All @@ -205,7 +209,7 @@ export function createDocumentMutationOptions<D extends MutableDocumentType>({
...baseMutationOptions(),
mutationFn: async ({
value,
}): Promise<MutableDocument<D> & { forks: Array<string> }> => {
}): Promise<WriteableDocument<D> & { forks: Array<string> }> => {
// @ts-expect-error TS not handling this well
return projectApi[docType].create({
...value,
Expand All @@ -221,13 +225,13 @@ export function createDocumentMutationOptions<D extends MutableDocumentType>({
})
},
} satisfies UseMutationOptions<
MutableDocument<D> & { forks: Array<string> },
WriteableDocument<D> & { forks: Array<string> },
Error,
{ value: Omit<MutableValue<D>, 'schemaName'> }
{ value: Omit<WriteableValue<D>, 'schemaName'> }
>
}

export function updateDocumentMutationOptions<D extends MutableDocumentType>({
export function updateDocumentMutationOptions<D extends WriteableDocumentType>({
docType,
projectApi,
projectId,
Expand All @@ -243,7 +247,7 @@ export function updateDocumentMutationOptions<D extends MutableDocumentType>({
mutationFn: async ({
versionId,
value,
}): Promise<MutableDocument<D> & { forks: Array<string> }> => {
}): Promise<WriteableDocument<D> & { forks: Array<string> }> => {
// @ts-expect-error TS not handling this well
return projectApi[docType].update(versionId, value)
},
Expand All @@ -256,16 +260,16 @@ export function updateDocumentMutationOptions<D extends MutableDocumentType>({
})
},
} satisfies UseMutationOptions<
MutableDocument<D> & { forks: Array<string> },
WriteableDocument<D> & { forks: Array<string> },
Error,
{
versionId: string
value: Omit<MutableValue<D>, 'schemaName'>
value: Omit<WriteableValue<D>, 'schemaName'>
}
>
}

export function deleteDocumentMutationOptions<D extends MutableDocumentType>({
export function deleteDocumentMutationOptions<D extends WriteableDocumentType>({
docType,
projectApi,
projectId,
Expand All @@ -280,7 +284,7 @@ export function deleteDocumentMutationOptions<D extends MutableDocumentType>({
...baseMutationOptions(),
mutationFn: async ({
docId,
}): Promise<MutableDocument<D> & { forks: Array<string> }> => {
}): Promise<WriteableDocument<D> & { forks: Array<string> }> => {
// @ts-expect-error TS not handling this well
return projectApi[docType].delete(docId)
},
Expand All @@ -293,7 +297,7 @@ export function deleteDocumentMutationOptions<D extends MutableDocumentType>({
})
},
} satisfies UseMutationOptions<
MutableDocument<D> & { forks: Array<string> },
WriteableDocument<D> & { forks: Array<string> },
Error,
{ docId: string }
>
Expand Down

0 comments on commit a69c1e6

Please sign in to comment.