i18next#TOptions TypeScript Examples

The following examples show how to use i18next#TOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param ids @example ids = ["id_1", "id_2"]
   * @param options
   */
  public static async multipleDelete(
    ids: string[],
    options?: SqlizeOptions
  ): Promise<void> {
    const i18nOpt: string | TOptions = { lng: options?.lang }
    const isForce = validateBoolean(options?.force)

    if (_.isEmpty(ids)) {
      const message = i18nConfig.t('errors.cantBeEmpty', i18nOpt)
      throw new ResponseError.BadRequest(`ids ${message}`)
    }

    await Notification.destroy({
      where: { id: { [Op.in]: ids } },
      force: isForce,
    })
  }
Example #2
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param email
   * @param options
   * @returns
   */
  public static async validateEmail(
    email: string,
    options?: SqlizeOptions
  ): Promise<null> {
    const i18nOpt: string | TOptions = { lng: options?.lang }
    const data = await User.findOne({ where: { email } })

    if (data) {
      const message = i18nConfig.t('errors.alreadyEmail', i18nOpt)
      throw new ResponseError.BadRequest(message)
    }

    return null
  }
Example #3
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param ids @example ids = ["id_1", "id_2"]
   * @param options
   */
  public static async multipleDelete(
    ids: string[],
    options?: SqlizeOptions
  ): Promise<void> {
    const i18nOpt: string | TOptions = { lng: options?.lang }
    const isForce = validateBoolean(options?.force)

    if (_.isEmpty(ids)) {
      const message = i18nConfig.t('errors.cantBeEmpty', i18nOpt)
      throw new ResponseError.BadRequest(`ids ${message}`)
    }

    await User.destroy({
      where: { id: { [Op.in]: ids } },
      force: isForce,
    })
  }
Example #4
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param ids @example ids = ["id_1", "id_2"]
   * @param options
   */
  public static async multipleRestore(
    ids: string[],
    options?: SqlizeOptions
  ): Promise<void> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    if (_.isEmpty(ids)) {
      const message = i18nConfig.t('errors.cantBeEmpty', i18nOpt)
      throw new ResponseError.BadRequest(`ids ${message}`)
    }

    await User.restore({
      where: { id: { [Op.in]: ids } },
    })
  }
Example #5
Source File: controller.ts    From expresso with MIT License 6 votes vote down vote up
route.post(
  '/auth/sign-up',
  asyncHandler(async function signUp(req: Request, res: Response) {
    const { lang } = req.getQuery()
    const defaultLang = lang ?? APP_LANG
    const i18nOpt: string | TOptions = { lng: lang }

    const formData = req.getBody()

    const data = await AuthService.signUp(formData, { lang: defaultLang })
    const message = i18nConfig.t('success.register', i18nOpt)

    const httpResponse = HttpResponse.created({ message, data })
    res.status(201).json(httpResponse)
  })
)
Example #6
Source File: controller.ts    From expresso with MIT License 6 votes vote down vote up
route.post(
  '/logout',
  Authorization,
  asyncHandler(async function logout(req: Request, res: Response) {
    const { lang } = req.getQuery()
    const defaultLang = lang ?? APP_LANG
    const i18nOpt: string | TOptions = { lng: lang }

    const formData = req.getBody()
    const getToken = currentToken(req)
    const userLogin = req.getState('userLogin') as UserLoginAttributes

    if (userLogin.uid !== formData.UserId) {
      const message = i18nConfig.t('errors.invalidUserLogin', i18nOpt)
      throw new ResponseError.BadRequest(message)
    }

    const message = await AuthService.logout(userLogin.uid, getToken, {
      lang: defaultLang,
    })
    const httpResponse = HttpResponse.get({ message })

    res.status(200).clearCookie('token', { path: '/v1' }).json(httpResponse)
  })
)
Example #7
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param UserId
   * @param token
   * @param options
   * @returns
   */
  public static async logout(
    UserId: string,
    token: string,
    options?: SqlizeOptions
  ): Promise<string> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    const getUser = await UserService.findById(UserId, { ...options })

    // clean session
    await SessionService.deleteByUserToken(getUser.id, token)
    const message = i18nConfig.t('success.logout', i18nOpt)

    return message
  }
Example #8
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param req Request
   */
  public static async findAll(req: Request): Promise<DtoPaginate> {
    const { lang } = req.getQuery()
    const defaultLang = lang ?? APP_LANG
    const i18nOpt: string | TOptions = { lng: defaultLang }

    const { includeCount, order, ...queryFind } = PluginSqlizeQuery.generate(
      req.query,
      Notification,
      []
    )

    const data = await Notification.findAll({
      ...queryFind,
      order: order.length ? order : [['createdAt', 'desc']],
    })
    const total = await Notification.count({
      include: includeCount,
      where: queryFind.where,
    })

    const message = i18nConfig.t('success.dataReceived', i18nOpt)
    return { message: `${total} ${message}`, data, total }
  }
Example #9
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param id
   * @param options
   * @returns
   */
  public static async findByPk(
    id: string,
    options?: SqlizeOptions
  ): Promise<NotificationInstance> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    const newId = validateUUID(id, { lang: options?.lang })
    const data = await Notification.findByPk(newId, {
      include: options?.include,
      order: options?.order,
      paranoid: options?.paranoid,
    })

    if (!data) {
      const message = i18nConfig.t('errors.notFound', i18nOpt)
      throw new ResponseError.NotFound(`notification ${message}`)
    }

    return data
  }
Example #10
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param id
   * @param options
   * @returns
   */
  public static async findByPk(
    id: string,
    options?: SqlizeOptions
  ): Promise<UserInstance> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    const newId = validateUUID(id, { lang: options?.lang })
    const data = await User.findByPk(newId, {
      include: options?.include,
      order: options?.order,
      paranoid: options?.paranoid,
    })

    if (!data) {
      const message = i18nConfig.t('errors.notFound', i18nOpt)
      throw new ResponseError.NotFound(`user ${message}`)
    }

    return data
  }
Example #11
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param ids @example ids = ["id_1", "id_2"]
   * @param options
   */
  public static async multipleRestore(
    ids: string[],
    options?: SqlizeOptions
  ): Promise<void> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    if (_.isEmpty(ids)) {
      const message = i18nConfig.t('errors.cantBeEmpty', i18nOpt)
      throw new ResponseError.BadRequest(`ids ${message}`)
    }

    await Notification.restore({
      where: { id: { [Op.in]: ids } },
    })
  }
Example #12
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param req
   * @returns
   */
  public static async findAll(req: Request): Promise<DtoPaginate> {
    const { lang } = req.getQuery()
    const defaultLang = lang ?? APP_LANG
    const i18nOpt: string | TOptions = { lng: defaultLang }

    const { includeCount, order, ...queryFind } = PluginSqlizeQuery.generate(
      req.query,
      Upload,
      []
    )

    const data = await Upload.findAll({
      ...queryFind,
      order: order.length ? order : [['createdAt', 'desc']],
    })
    const total = await Upload.count({
      include: includeCount,
      where: queryFind.where,
    })

    const message = i18nConfig.t('success.dataReceived', i18nOpt)
    return { message: `${total} ${message}`, data, total }
  }
Example #13
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param id
   * @param options
   * @returns
   */
  public static async findByPk(
    id: string,
    options?: SqlizeOptions
  ): Promise<UploadInstance> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    const newId = validateUUID(id, { lang: options?.lang })
    const data = await Upload.findByPk(newId, {
      include: options?.include,
      order: options?.order,
      paranoid: options?.paranoid,
    })

    if (!data) {
      const message = i18nConfig.t('errors.notFound', i18nOpt)
      throw new ResponseError.NotFound(`upload ${message}`)
    }

    return data
  }
Example #14
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param ids @example ids = ["id_1", "id_2"]
   * @param options
   */
  public static async multipleDelete(
    ids: string[],
    options?: SqlizeOptions
  ): Promise<void> {
    const i18nOpt: string | TOptions = { lng: options?.lang }
    const isForce = validateBoolean(options?.force)

    if (_.isEmpty(ids)) {
      const message = i18nConfig.t('errors.cantBeEmpty', i18nOpt)
      throw new ResponseError.BadRequest(`ids ${message}`)
    }

    await Upload.destroy({
      where: { id: { [Op.in]: ids } },
      force: isForce,
    })
  }
Example #15
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param ids @example ids = ["id_1", "id_2"]
   * @param options
   */
  public static async multipleRestore(
    ids: string[],
    options?: SqlizeOptions
  ): Promise<void> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    if (_.isEmpty(ids)) {
      const message = i18nConfig.t('errors.cantBeEmpty', i18nOpt)
      throw new ResponseError.BadRequest(`ids ${message}`)
    }

    await Upload.restore({
      where: { id: { [Op.in]: ids } },
    })
  }
Example #16
Source File: NotPermittedAccess.ts    From expresso with MIT License 6 votes vote down vote up
function NotPermittedAccess(roles: string[]) {
  return async (req: Request, res: Response, next: NextFunction) => {
    const { lang } = req.getQuery()
    const defaultLang = lang ?? APP_LANG

    const i18nOpt: string | TOptions = { lng: defaultLang }

    const userLogin = req.getState('userLogin') as UserLoginAttributes
    const getUser = await User.findByPk(userLogin.uid)

    const errType = `Not Permitted Access Error:`
    const message = 'You are not allowed'

    if (getUser && roles.includes(getUser.RoleId)) {
      // log error
      console.log(logErrServer(errType, message))

      const errMessage = i18nConfig.t('errors.permissionAccess', i18nOpt)
      const httpResponse = HttpResponse.get({
        code: 403,
        message: errMessage,
      })

      return res.status(403).json(httpResponse)
    }

    next()
  }
}
Example #17
Source File: PermissionAccess.ts    From expresso with MIT License 6 votes vote down vote up
function PermissionAccess(roles: string[]) {
  return async (req: Request, res: Response, next: NextFunction) => {
    const { lang } = req.getQuery()
    const defaultLang = lang ?? APP_LANG

    const i18nOpt: string | TOptions = { lng: defaultLang }

    const userLogin = req.getState('userLogin') as UserLoginAttributes
    const getUser = await User.findByPk(userLogin.uid)

    const errType = `Permission Access Error:`
    const message = 'You are not allowed'

    if (getUser && !roles.includes(getUser.RoleId)) {
      // log error
      console.log(logErrServer(errType, message))

      const errMessage = i18nConfig.t('errors.permissionAccess', i18nOpt)
      const httpResponse = HttpResponse.get({
        code: 403,
        message: errMessage,
      })

      return res.status(403).json(httpResponse)
    }

    next()
  }
}
Example #18
Source File: useTranslationSafe.ts    From nextclade with MIT License 6 votes vote down vote up
export function useTranslationSafe<TInterpolationMap extends object = StringMap>() {
  const response = useTranslation()

  function t(key: string, options?: TOptions<TInterpolationMap> | string) {
    return response.t(key, options) ?? key
  }

  return { ...response, t }
}
Example #19
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param id
   * @param options
   * @returns
   */
  public static async findByPk(
    id: string,
    options?: SqlizeOptions
  ): Promise<RoleInstance> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    const newId = validateUUID(id, { lang: options?.lang })
    const data = await Role.findByPk(newId, {
      include: options?.include,
      order: options?.order,
      paranoid: options?.paranoid,
    })

    if (!data) {
      const message = i18nConfig.t('errors.notFound', i18nOpt)
      throw new ResponseError.NotFound(`role ${message}`)
    }

    return data
  }
Example #20
Source File: lang.ts    From brackets-viewer.js with MIT License 6 votes vote down vote up
/**
 * Returns an internationalized version of a locale key.
 * 
 * @param key A locale key.
 * @param options Data to pass to the i18n process.
 */
export function t<Scope extends keyof Locale, SubKey extends string & keyof Locale[Scope], T extends TOptions>(
    key: `${Scope}.${SubKey}`, options?: T,
): T['returnObjects'] extends true ? StringMap : string {
    return i18next.t(key, options);
}
Example #21
Source File: Formatter.ts    From expresso with MIT License 6 votes vote down vote up
/**
 *
 * @param value
 * @param options
 * @returns
 */
function validateUUID(value: string, options?: SqlizeOptions): string {
  const i18nOpt: string | TOptions = { lng: options?.lang }

  if (!uuidValidate(value)) {
    const message = i18nConfig.t('errors.incorrectUUIDFormat', i18nOpt)
    throw new ResponseError.BadRequest(message)
  }

  return value
}
Example #22
Source File: SendMail.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param formData
   * @param lang
   */
  public static AccountRegistration(
    formData: AccountRegistrationProps,
    lang?: string
  ): void {
    const i18nOpt: string | TOptions = { lng: lang }

    const templatePath = path.resolve(
      `${__dirname}/../../../public/templates/emails/register.html`
    )
    console.log({ templatePath })

    const subject = 'Email Verification'
    const tokenUrl = `${BASE_URL_SERVER}/v1/email/verify?token=${formData.token}`
    const templateData = { APP_NAME, tokenUrl, ...formData }

    if (!fs.existsSync(templatePath)) {
      const message = i18nConfig.t('errors.mailTemplate', i18nOpt)
      throw new ResponseError.BadRequest(message)
    }

    // read html template email
    readHTMLFile(templatePath, (err: Error, html: any) => {
      if (err) console.log(err)

      const template = Handlebars.compile(html)
      const htmlToSend = template(templateData)

      SMTPEmail.send(formData.email, subject, htmlToSend)
    })
  }
Example #23
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param req
   * @returns
   */
  public static async findAll(req: Request): Promise<DtoPaginate> {
    const { lang } = req.getQuery()
    const defaultLang = lang ?? APP_LANG
    const i18nOpt: string | TOptions = { lng: defaultLang }

    const { includeCount, order, ...queryFind } = PluginSqlizeQuery.generate(
      req.query,
      FCMToken,
      []
    )

    const data = await FCMToken.findAll({
      ...queryFind,
      order: order.length ? order : [['createdAt', 'desc']],
    })
    const total = await FCMToken.count({
      include: includeCount,
      where: queryFind.where,
    })

    const message = i18nConfig.t('success.dataReceived', i18nOpt)
    return { message: `${total} ${message}`, data, total }
  }
Example #24
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param id
   * @param options
   * @returns
   */
  public static async findByPk(
    id: string,
    options?: SqlizeOptions
  ): Promise<FCMTokenInstance> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    const newId = validateUUID(id, { lang: options?.lang })
    const data = await FCMToken.findByPk(newId, {
      include: options?.include,
      order: options?.order,
      paranoid: options?.paranoid,
    })

    if (!data) {
      const message = i18nConfig.t('errors.notFound', i18nOpt)
      throw new ResponseError.NotFound(`fcm token ${message}`)
    }

    return data
  }
Example #25
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param condition
   * @param options
   * @returns
   */
  public static async findByCondition(
    condition: WhereOptions<FCMTokenAttributes>,
    options?: SqlizeOptions
  ): Promise<FCMTokenInstance> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    const data = await FCMToken.findOne({
      where: condition,
      include: options?.include,
      order: options?.order,
      paranoid: options?.paranoid,
    })

    if (!data) {
      const message = i18nConfig.t('errors.notFound', i18nOpt)
      throw new ResponseError.NotFound(`fcm token ${message}`)
    }

    return data
  }
Example #26
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param ids @example ids = ["id_1", "id_2"]
   * @param options
   */
  public static async multipleDelete(
    ids: string[],
    options?: SqlizeOptions
  ): Promise<void> {
    const i18nOpt: string | TOptions = { lng: options?.lang }
    const isForce = validateBoolean(options?.force)

    if (_.isEmpty(ids)) {
      const message = i18nConfig.t('errors.cantBeEmpty', i18nOpt)
      throw new ResponseError.BadRequest(`ids ${message}`)
    }

    await FCMToken.destroy({
      where: { id: { [Op.in]: ids } },
      force: isForce,
    })
  }
Example #27
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param ids @example ids = ["id_1", "id_2"]
   * @param options
   */
  public static async multipleRestore(
    ids: string[],
    options?: SqlizeOptions
  ): Promise<void> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    if (_.isEmpty(ids)) {
      const message = i18nConfig.t('errors.cantBeEmpty', i18nOpt)
      throw new ResponseError.BadRequest(`ids ${message}`)
    }

    await FCMToken.restore({
      where: { id: { [Op.in]: ids } },
    })
  }
Example #28
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param req
   * @returns
   */
  public static async findAll(req: Request): Promise<DtoPaginate> {
    const { lang } = req.getQuery()
    const defaultLang = lang ?? APP_LANG
    const i18nOpt: string | TOptions = { lng: defaultLang }

    const { includeCount, order, ...queryFind } = PluginSqlizeQuery.generate(
      req.query,
      Role,
      []
    )

    const data = await Role.findAll({
      ...queryFind,
      order: order.length ? order : [['createdAt', 'desc']],
    })
    const total = await Role.count({
      include: includeCount,
      where: queryFind.where,
    })

    const message = i18nConfig.t('success.dataReceived', i18nOpt)
    return { message: `${total} ${message}`, data, total }
  }
Example #29
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param ModelEntity
   * @param target
   * @param options
   */
  private static async validateDelete<T>(
    ModelEntity: T[],
    target: string,
    options?: SqlizeOptions
  ): Promise<void> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    if (!_.isEmpty(ModelEntity)) {
      const collectRoleIds = _.map(ModelEntity, 'RoleId')
      const uniqRoleIds = [...new Set(collectRoleIds)]

      const getRoles = await Role.findAll({
        where: { id: { [Op.in]: uniqRoleIds } },
      })

      if (!_.isEmpty(getRoles)) {
        const collectRoles = _.map(getRoles, 'name')
        const getName = collectRoles.join(', ')

        const message = i18nConfig.t('errors.isBeingUsed', i18nOpt)
        throw new ResponseError.BadRequest(
          `Role ${getName} ${message} ${target}`
        )
      }
    }
  }