Skip to content

Commit

Permalink
refactor: reduce usage of lodash
Browse files Browse the repository at this point in the history
- import from lodash submodules directly where possible
  • Loading branch information
fratzinger authored and daffl committed May 3, 2024
1 parent 44d4b09 commit 9629cde
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 25 deletions.
5 changes: 3 additions & 2 deletions packages/authentication-local/src/strategy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import bcrypt from 'bcryptjs'
import get from 'lodash/get'
import omit from 'lodash/omit'
import { NotAuthenticated } from '@feathersjs/errors'
import { Query, Params } from '@feathersjs/feathers'
import { AuthenticationRequest, AuthenticationBaseStrategy } from '@feathersjs/authentication'
Expand Down Expand Up @@ -130,7 +129,9 @@ export class LocalStrategy extends AuthenticationBaseStrategy {
throw new NotAuthenticated(errorMessage)
}

const result = await this.findEntity(username, omit(params, 'provider'))
const { provider, ...paramsWithoutProvider } = params

const result = await this.findEntity(username, paramsWithoutProvider)
await this.comparePassword(result, password)

return {
Expand Down
2 changes: 0 additions & 2 deletions packages/authentication-oauth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@
"cookie-session": "^2.1.0",
"grant": "^5.4.22",
"koa-session": "^6.4.0",
"lodash": "^4.17.21",
"qs": "^6.12.0"
},
"devDependencies": {
"@feathersjs/memory": "^5.0.24",
"@types/cookie-session": "^2.0.48",
"@types/express": "^4.17.21",
"@types/koa-session": "^6.4.5",
"@types/lodash": "^4.17.0",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.26",
"@types/tough-cookie": "^4.0.5",
Expand Down
16 changes: 10 additions & 6 deletions packages/authentication-oauth/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import koaCookieSession from 'koa-session'
import { AuthenticationService } from '@feathersjs/authentication'
import { GrantConfig } from 'grant'

import { defaultsDeep, each, omit } from 'lodash'

export interface OauthSetupSettings {
linkStrategy: string
authService?: string
Expand All @@ -38,25 +36,31 @@ export const getGrantConfig = (service: AuthenticationService): GrantConfig => {
}
}

const grant: GrantConfig = defaultsDeep({}, omit(oauth, ['redirect', 'origins']), {
// omit 'redirect' and 'origins' from oauth
const { redirect, origins, ...oauthConfig } = oauth

const grant: GrantConfig = {
...oauthConfig,
defaults: {
...oauthConfig.defaults,
prefix: '/oauth',
origin: `${protocol}://${host}`,
transport: 'state',
response: ['tokens', 'raw', 'profile']
}
})
}

const getUrl = (url: string) => {
const { defaults } = grant
return `${defaults.origin}${defaults.prefix}/${url}`
}

each(grant, (value, name) => {
// iterate over grant object with key and value
for (const [name, value] of Object.entries(grant)) {
if (name !== 'defaults') {
value.redirect_uri = value.redirect_uri || getUrl(`${name}/callback`)
}
})
}

return grant
}
Expand Down
14 changes: 8 additions & 6 deletions packages/authentication/src/hooks/authenticate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import flatten from 'lodash/flatten'
import omit from 'lodash/omit'
import { HookContext, NextFunction } from '@feathersjs/feathers'
import { NotAuthenticated } from '@feathersjs/errors'
import { createDebug } from '@feathersjs/commons'
Expand All @@ -14,7 +12,7 @@ export interface AuthenticateHookSettings {
export default (originalSettings: string | AuthenticateHookSettings, ...originalStrategies: string[]) => {
const settings =
typeof originalSettings === 'string'
? { strategies: flatten([originalSettings, ...originalStrategies]) }
? { strategies: [originalSettings, ...originalStrategies] }
: originalSettings

if (!originalSettings || settings.strategies.length === 0) {
Expand Down Expand Up @@ -49,15 +47,19 @@ export default (originalSettings: string | AuthenticateHookSettings, ...original
}

if (authentication) {
const authParams = omit(params, 'provider', 'authentication')
const { provider, authentication, ...authParams } = params

debug('Authenticating with', authentication, strategies)

const authResult = await authService.authenticate(authentication, authParams, ...strategies)

context.params = Object.assign({}, params, omit(authResult, 'accessToken'), {
const { accessToken, ...authResultWithoutToken } = authResult

context.params = {
...params,
...authResultWithoutToken,
authenticated: true
})
}
} else if (provider) {
throw new NotAuthenticated('Not authenticated')
}
Expand Down
8 changes: 5 additions & 3 deletions packages/authentication/src/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/ban-ts-comment */
import omit from 'lodash/omit'
import { IncomingMessage } from 'http'
import { NotAuthenticated } from '@feathersjs/errors'
import { Params } from '@feathersjs/feathers'
Expand Down Expand Up @@ -115,8 +114,11 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
}

const query = await this.getEntityQuery(params)
const getParams = Object.assign({}, omit(params, 'provider'), { query })
const result = await entityService.get(id, getParams)
const { provider, ...paramsWithoutProvider } = params
const result = await entityService.get(id, {
...paramsWithoutProvider,
query
})

if (!params.provider) {
return result
Expand Down
2 changes: 1 addition & 1 deletion packages/express/test/authentication.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { omit } from 'lodash'
import omit from 'lodash/omit'
import { strict as assert } from 'assert'
import { default as _axios } from 'axios'
import { feathers } from '@feathersjs/feathers'
Expand Down
3 changes: 2 additions & 1 deletion packages/socketio/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
} from '@feathersjs/feathers'
import express from '@feathersjs/express'
import { Request, Response } from 'express'
import { omit, extend } from 'lodash'
import omit from 'lodash/omit'
import extend from 'lodash/extend'
import { io } from 'socket.io-client'
import axios from 'axios'
import { Server } from 'http'
Expand Down
8 changes: 5 additions & 3 deletions packages/transport-commons/src/channels/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Application, FeathersService, RealTimeConnection, getServiceOptions } from '@feathersjs/feathers'
import { createDebug } from '@feathersjs/commons'
import { compact, flattenDeep, noop } from 'lodash'
import flattenDeep from 'lodash/flattenDeep'
import { Channel } from './channel/base'
import { CombinedChannel } from './channel/combined'
import { channelMixin, publishMixin, keys, PublishMixin, Event, Publisher } from './mixins'
Expand Down Expand Up @@ -87,7 +87,7 @@ export function channels() {
// 4. App publisher for all events
appPublishers[keys.ALL_EVENTS] ||
// 5. No publisher
noop
(() => {})

try {
Promise.resolve(publisher(data, hook))
Expand All @@ -96,7 +96,9 @@ export function channels() {
return
}

const results = Array.isArray(result) ? compact(flattenDeep(result)) : ([result] as Channel[])
const results = Array.isArray(result)
? flattenDeep(result).filter(Boolean)
: ([result] as Channel[])
const channel = new CombinedChannel(results)

if (channel && channel.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/transport-commons/test/socket/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert'
import { EventEmitter } from 'events'
import { feathers, Application, Params, RealTimeConnection } from '@feathersjs/feathers'
import { NotAuthenticated } from '@feathersjs/errors'
import { isPlainObject } from 'lodash'
import isPlainObject from 'lodash/isPlainObject
import { routing } from '../../src/routing'
import { normalizeError, getDispatcher, runMethod } from '../../src/socket/utils'
Expand Down

0 comments on commit 9629cde

Please sign in to comment.