Skip to content

Commit

Permalink
feat!: allow set operations on meta
Browse files Browse the repository at this point in the history
BREAKING CHANGE: if you were setting directly `route.meta` within
`extendRoute()`, you know need to use `route.addToMeta()` instead to
have the same merging behavior. Directly setting `route.meta` now
replaces the `meta` property completely.
  • Loading branch information
posva committed Feb 16, 2023
1 parent 7ce4e2f commit a84d659
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/core/extendRoutes.ts
Expand Up @@ -93,16 +93,25 @@ export class EditableTreeNode {
/**
* Meta property of the route as an object. Note this property is readonly and will be serialized as JSON. It won't contain the meta properties defined with `definePage()` as it could contain expressions **but it does contain the meta properties defined with `<route>` blocks**.
*/
get meta() {
get meta(): Readonly<RouteMeta> {
return this.node.metaAsObject
}

/**
* Override the meta property of the route. The passed object will be deeply merged with the existing meta object if any.
* Note that the meta property is later on serialized as JSON so you can't pass functions or any other
* non-serializable value.
* Override the meta property of the route. This will discard any other meta property defined with `<route>` blocks or
* through other means.
*/
set meta(meta: RouteMeta) {
this.node.value.removeOverride('meta')
this.node.value.setEditOverride('meta', meta)
}

/**
* Add meta properties to the route keeping the existing ones. The passed object will be deeply merged with the
* existing meta object if any. Note that the meta property is later on serialized as JSON so you can't pass functions
* or any other non-serializable value.
*/
addToMeta(meta: Partial<RouteMeta>) {
this.node.value.addEditOverride({ meta })
}

Expand Down
13 changes: 13 additions & 0 deletions src/core/treeNodeValue.ts
Expand Up @@ -129,6 +129,19 @@ class _TreeNodeValueBase {
addEditOverride(routeBlock: CustomRouteBlock) {
return this.mergeOverride(EDITS_OVERRIDE_NAME, routeBlock)
}

setEditOverride<K extends keyof RouteRecordOverride>(
key: K,
value: RouteRecordOverride[K]
) {
// return this.mergeOverride(EDITS_OVERRIDE_NAME, routeBlock)
if (!this._overrides.has(EDITS_OVERRIDE_NAME)) {
this._overrides.set(EDITS_OVERRIDE_NAME, {})
}

const existing = this._overrides.get(EDITS_OVERRIDE_NAME)!
existing[key] = value
}
}

export class TreeNodeValueStatic extends _TreeNodeValueBase {
Expand Down

0 comments on commit a84d659

Please sign in to comment.