Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: omitServerProps helper function #277

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"dependencies": {
"@comapeo/geometry": "^1.0.2",
"compact-encoding": "^2.12.0",
"filter-obj": "^6.1.0",
"protobufjs": "^7.2.5",
"type-fest": "^4.26.0"
},
Expand Down
3 changes: 3 additions & 0 deletions scripts/lib/generate-jsonschema-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export async function generateJSONSchemaExports(jsonSchemas) {

return `import { type JSONSchema7 } from 'json-schema'
import { type SchemaNameAll } from './types.js'
import { type MapeoCommon } from './schema/index.js'
import { type ReadonlyDeep } from 'type-fest'

declare module 'json-schema' {
Expand All @@ -58,6 +59,8 @@ declare module 'json-schema' {

type SchemaRecord = Record<SchemaNameAll, ReadonlyDeep<JSONSchema7>>

export const commonSchema = ${printf(jsonSchemas.values.common)} as const satisfies ReadonlyDeep<JSONSchema7>

export const docSchemas = ${printf(docSchemas)} as const satisfies SchemaRecord

export const dereferencedDocSchemas = ${printf(
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
getVersionId,
parseVersionId,
valueOf,
omitServerProps,
type VersionIdObject,
} from './lib/utils.js'

Expand Down
28 changes: 27 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { excludeKeys } from 'filter-obj'
import { type ProtoTypeNames } from '../proto/types.js'
import {
type FilterBySchemaName,
type MapeoCommon,
type MapeoDoc,
type MapeoValue,
type FilterBySchemaName,
} from '../types.js'
import { commonSchema } from '../schemas.js'
import { omit } from './omit.js'

export function getOwn<T extends object, K extends keyof T>(
Expand Down Expand Up @@ -79,6 +82,10 @@ export function parseVersionId(versionId: string): VersionIdObject {
return { coreDiscoveryKey, index }
}

/**
* Get the value of a document, excluding server-managed properties.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is a little unclear because it makes it look like it should do the same thing as the other function.

* @deprecated Use the more flexible `omitServerProps` instead
*/
export function valueOf<TDoc extends MapeoDoc>(
doc: TDoc & { forks?: string[] }
): FilterBySchemaName<MapeoValue, TDoc['schemaName']> {
Expand All @@ -93,3 +100,22 @@ export function valueOf<TDoc extends MapeoDoc>(
'deleted',
]) as any
}

// Casting this type because we _know_ that commonSchema.properties does not
// have additional keys, unlike situations described in
// https://stackoverflow.com/a/55012175
const commonSchemaKeys = Object.keys(commonSchema.properties).filter(
(key) => key !== 'schemaName'
) as Array<keyof Omit<MapeoCommon, 'schemaName'>>

const keysToExclude = [...commonSchemaKeys, 'forks'] as const

/**
* Omit server-managed properties from a document, such as `createdBy`,
* `createdAt`, etc. These properties cannot be modified by the client.
*/
export function omitServerProps<TDoc extends MapeoCommon>(
doc: TDoc & { forks?: string[] }
) {
return excludeKeys(doc, keysToExclude)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use our existing omit helper (and maybe remove the new filter-obj dependency)?

}
Loading