Skip to content
Draft
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
26 changes: 26 additions & 0 deletions imports/experimental/create-minilinks-decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { MinilinkCollection } from "../minilinks"

export function createMinilinksDecorator<TMinilinks extends MinilinkCollection>(minilinks: TMinilinks): MinilinksDecorator<TMinilinks> {
const result: MinilinksDecorator<TMinilinks> = Object.assign({
id: function (this: MinilinksDecorator<TMinilinks>, pathItems: [string, ...Array<string>]) {
const result = this.idOrNull(pathItems)
if(result === null) throw new Error(`Minilink Error: id not found by path ${pathItems.join('/')}`)
return result
},
idOrNull: function (this: TMinilinks, pathItems: [string, ...Array<string>]) {
const result = this.query({
id: {
_id: pathItems
}
})
if (result.length === 0) return null;
return result[0].id
}
} , minilinks)
return result;
}

export type MinilinksDecorator<TMinilinks extends MinilinkCollection> = TMinilinks & {
id(this: MinilinksDecorator<TMinilinks>, pathItems: [string, ...Array<string>]): number;
idOrNull(this: MinilinksDecorator<TMinilinks>, pathItems: [string, ...Array<string>]): number | null;
}
2 changes: 1 addition & 1 deletion imports/minilinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function Minilinks<MGO extends MinilinksGeneratorOptions, L extends Link<

export interface MinilinkError extends Error {}

export class MinilinkCollection<MGO extends MinilinksGeneratorOptions, L extends Link<number>> {
export class MinilinkCollection<MGO extends MinilinksGeneratorOptions = typeof MinilinksGeneratorOptionsDefault, L extends Link<number> = Link<number>> {
useMinilinksQuery = useMinilinksQuery;
useMinilinksFilter = useMinilinksFilter;
useMinilinksApply = useMinilinksApply;
Expand Down