diff --git a/package-lock.json b/package-lock.json index ea40628..7f8c981 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,8 +13,7 @@ "ajv": "^6.12.6", "debug": "^4.3.4", "graphql": "^16.8.1", - "lodash": "^4.17.21", - "traverse": "^0.6.8" + "lodash": "^4.17.21" }, "devDependencies": { "@feathers-plus/batch-loader": "^0.3.6", @@ -10009,6 +10008,7 @@ "version": "0.6.8", "resolved": "https://registry.npmjs.org/traverse/-/traverse-0.6.8.tgz", "integrity": "sha512-aXJDbk6SnumuaZSANd21XAo15ucCDE38H4fkqiGsc3MhCK+wOlZvLP9cB/TvpHT0mOyWgC4Z8EwRlzqYSUzdsA==", + "dev": true, "engines": { "node": ">= 0.4" }, @@ -18591,7 +18591,8 @@ "traverse": { "version": "0.6.8", "resolved": "https://registry.npmjs.org/traverse/-/traverse-0.6.8.tgz", - "integrity": "sha512-aXJDbk6SnumuaZSANd21XAo15ucCDE38H4fkqiGsc3MhCK+wOlZvLP9cB/TvpHT0mOyWgC4Z8EwRlzqYSUzdsA==" + "integrity": "sha512-aXJDbk6SnumuaZSANd21XAo15ucCDE38H4fkqiGsc3MhCK+wOlZvLP9cB/TvpHT0mOyWgC4Z8EwRlzqYSUzdsA==", + "dev": true }, "tree-kill": { "version": "1.2.2", diff --git a/package.json b/package.json index 48cd675..99c0068 100644 --- a/package.json +++ b/package.json @@ -66,8 +66,7 @@ "ajv": "^6.12.6", "debug": "^4.3.4", "graphql": "^16.8.1", - "lodash": "^4.17.21", - "traverse": "^0.6.8" + "lodash": "^4.17.21" }, "devDependencies": { "@feathers-plus/batch-loader": "^0.3.6", @@ -110,4 +109,4 @@ "engines": { "node": ">= 18" } -} +} \ No newline at end of file diff --git a/src/common/index.ts b/src/common/index.ts index d1efe96..e5f8d7e 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -5,5 +5,4 @@ export function isPromise(p: any): p is Promise { export { pluck } from './pluck'; export { setFields } from './set-fields'; export { transformItems } from './transform-items'; -export { traverse } from './traverse'; export { clone } from './clone'; diff --git a/src/common/traverse.ts b/src/common/traverse.ts deleted file mode 100755 index 18417cf..0000000 --- a/src/common/traverse.ts +++ /dev/null @@ -1,10 +0,0 @@ -import traverser from 'traverse'; - -export function traverse>( - items: T | T[], - converter: (item: T) => void, -) { - (Array.isArray(items) ? items : [items]).forEach(item => { - traverser(item).forEach(converter); // replacement is in place - }); -} diff --git a/src/hooks/mongo-keys.ts b/src/hooks/mongo-keys.ts deleted file mode 100755 index 0025766..0000000 --- a/src/hooks/mongo-keys.ts +++ /dev/null @@ -1,67 +0,0 @@ -import type { HookContext } from '@feathersjs/feathers'; -import traverse from 'traverse'; -import { checkContext } from '../utils/check-context'; - -/** - * Wrap MongoDB foreign keys in ObjectId. - * - * @see https://hooks-common.feathersjs.com/hooks.html#mongokeys - */ -export function mongoKeys( - ObjectId: new (id?: string | number) => any, - keyFields: string | string[], -) { - keyFields = Array.isArray(keyFields) ? keyFields : [keyFields]; - const keyLeaves: any = []; - - const keysInfo = keyFields.map((field: any) => { - const fieldNames = field.split('.'); - const leaf = fieldNames.slice(-1)[0]; - keyLeaves.push(leaf); - - return { leaf, len: fieldNames.length, path: JSON.stringify(fieldNames) }; - }); - - return (context: H) => { - checkContext(context, 'before', null, 'mongoKeys'); - const query = context.params.query || {}; - - traverse(query).forEach(function (this: any, node: any) { - const typeofNode = typeof node; - const key = this.key; - const path = this.path; - - if (keyLeaves.indexOf(key) === -1) return; - if (path.indexOf('$sort') !== -1) return; - - keysInfo.forEach((info: any) => { - if (info.leaf === key && info.len <= path.length) { - const endPath = path.slice(-info.len); - if (JSON.stringify(endPath) === info.path) { - if (typeofNode === 'object' && node !== null && !Array.isArray(node)) { - // { keyPath: { ... } } - const actualProps = Object.keys(node); - const onlyProp = actualProps[0]; - - if (actualProps.length === 1 && onlyProp === '$in') { - // { keyPath: { $in: [...] } } - const newNode = { $in: wrapValue(node[onlyProp]) }; - this.update(newNode); - } - } else if (typeofNode === 'string' || typeofNode === 'number') { - // { keyPath: '111111111111' } - const newNode = wrapValue(node); - this.update(newNode); - } - } - } - }); - }); - - return context; - }; - - function wrapValue(value: any) { - return Array.isArray(value) ? value.map(val => new ObjectId(val)) : new ObjectId(value); - } -} diff --git a/src/hooks/traverse.ts b/src/hooks/traverse.ts deleted file mode 100755 index 7666f25..0000000 --- a/src/hooks/traverse.ts +++ /dev/null @@ -1,22 +0,0 @@ -import type { HookContext } from '@feathersjs/feathers'; -import { traverse as _traverse } from '../common'; -import type { SyncContextFunction } from '../types'; -import { getItems } from '../utils/get-items'; - -/** - * Transform fields & objects in place in the record(s) using a recursive walk. Powerful. - * Check docs at https://github.com/substack/js-traverse for info on transformContext! - * @see https://hooks-common.feathersjs.com/hooks.html#traverse - */ -export function traverse( - transformer: (transformContext: any) => any, - getObject?: SyncContextFunction, -) { - return (context: H) => { - const items = - typeof getObject === 'function' ? getObject(context) : getObject || getItems(context); - - _traverse(items, transformer); - return context; - }; -} diff --git a/src/index.ts b/src/index.ts index 4aa4772..17f83a1 100755 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,6 @@ export * from './hooks/keep-in-array'; export * from './hooks/keep-query'; export * from './hooks/keep-query-in-array'; export * from './hooks/lower-case'; -// export * from './hooks/mongo-keys'; export * from './hooks/params-from-client'; export * from './hooks/populate'; export * from './hooks/prevent-changes'; @@ -32,7 +31,6 @@ export * from './hooks/set-slug'; export * from './hooks/sifter'; export * from './hooks/soft-delete'; export * from './hooks/stash-before'; -// export * from './hooks/traverse'; export * from './hooks/unless'; export * from './hooks/validate'; export * from './hooks/validate-schema';