1.0.0-rc.32 • Published 3 months ago

@exobase/use-path-params v1.0.0-rc.32

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

title: 'usePathParams' description: 'A hook to validate request path parameters'

group: 'Hooks'

An Exobase hook that validates path parameters in the request.

Install

yarn add @exobase/use-path-params
# or
yarn add @exobase/hooks

Import

import { usePathParams } from '@exobase/use-path-params'
// or
import { usePathParams } from '@exobase/hooks'

Usage

import { compose } from 'radash'
import type { Props } from '@exobase/core'
import { useNext } from '@exobase/use-next'
import { usePathParams } from '@exobase/use-path-params'

export const findBookById = async (props: Props) => {
  return db.books.find(props.args.bookId)
}

export default compose(
  useNext(),
  usePathParams(z => ({
    libraryId: z.string(),
    bookId: z.string().tranform(id => parseInt(id)),
  }))
  findBookById
)

when called with

curl /api/libraries/ny-public-library/books/3328

In some cases, you may need to parse the path params first. Sometimes the framework + root hook will do the param parsing. In other cases the useRouter hook, if you use it, will do the param parsing. If you're using AWS Lambda (without a router) there is no built in path param utility so you'll need to parse the path params manually using usePathParamsParser.

import { compose } from 'radash'
import type { Props } from '@exobase/core'
import { useLambda } from '@exobase/use-lambda'
import { usePathParamsParser } from '@exobase/use-path-params-parser'
import { usePathParams } from '@exobase/use-path-params'

export const findBookById = async (props: Props) => {
  return db.books.find(props.args.bookId)
}

export default compose(
  useLambda(),
  usePathParamsParser('/libraries/{libraryId}/books/{bookId}'),
  usePathParams(z => ({
    libraryId: z.string(),
    bookId: z.string().tranform(id => parseInt(id)),
  }))
  findBookById
)
1.0.0-rc.32

3 months ago

1.0.0-rc.31

3 months ago

1.0.0-rc.28

3 months ago

1.0.0-rc.27

3 months ago

1.0.0-rc.29

3 months ago

1.0.0-rc.30

3 months ago

1.0.0-rc.25

9 months ago

1.0.0-rc.24

1 year ago

1.0.0-rc.23

1 year ago

1.0.0-rc.22

1 year ago

1.0.0-rc.21

1 year ago

1.0.0-rc.20

1 year ago

1.0.0-rc.19

1 year ago

1.0.0-rc.13

1 year ago

1.0.0-rc.17

1 year ago

1.0.0-rc.16

1 year ago

1.0.0-rc.15

1 year ago

1.0.0-rc.14

1 year ago

1.0.0-rc.18

1 year ago

1.0.0-rc.12

1 year ago

1.0.0-rc.11

1 year ago

1.0.0-rc.10

1 year ago

1.0.0-rc.9

1 year ago

1.0.0-rc.8

1 year ago

1.0.0-rc.7

1 year ago

1.0.0-rc.6

2 years ago

1.0.0-rc.5

2 years ago

1.0.0-rc.4

2 years ago

1.0.0-rc.3

2 years ago

1.0.0-rc.2

2 years ago