Skip to content

πŸ”Ž Tiny and strongly typed request query validator and parser

License

Notifications You must be signed in to change notification settings

ArturSharapov/requery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Requery πŸ¦•

A tiny module for retrieving and validating queries (search params) from a given request, returning a strongly typed object of queries and their values.

Usage

import { getQueries } from 'https://deno.land/x/[email protected]/mod.ts'

getQueries(request, 'title', 'n:count', 'info?', 'n:ttl?', 'b:all', 'b:new')
// =>
{
  title: string,
  count: number,
  info?: string,
  ttl?: number
  all: boolean,
  new: boolean,
}

// https://example.com/?title=Dinosaur&count=5&ttl=26&all
// =>
{
  title: 'Dinosaur',
  count: 5,
  info: undefined,
  ttl: 26,
  all: true,
  new: false
}

Partial queries

All queries are optional by default and are not included to the final object if they do not present in the request.

import { getPartialQueries } from 'https://deno.land/x/[email protected]/mod.ts'

getPartialQueries(request, 'title', 'n:count', 'info', 'n:ttl', 'b:all', 'b:new')
// =>
{
  title?: string,
  count?: number,
  info?: string,
  ttl?: number
  all?: true,
  new?: true,
}

// https://example.com/?title=Dinosaur&count=5&ttl=26&all
// =>
{
  title: 'Dinosaur',
  count: 5,
  ttl: 26,
  all: true,
}

! Notice that info and new are not included.

Examples

const request = new Request('https://example.com/?r=255&g=126&b=32&extra=not-picked')
getQueries(request, 'n:r', 'n:g', 'n:b')
// =>
{
  r: 255,
  g: 126,
  b: 32
}
const request = new Request('https://example.com/?red=255&green=126')
getQueries(request, 'n:red', 'n:green', 'n:blue')
// =>
// InvalidParamsError: Param 'blue' is missing
const request = new Request('https://example.com/?confirmed=ok')
getQueries(request, 'b:confirmed')
// =>
// InvalidParamsError: Param 'confirmed' is not a boolean

About

πŸ”Ž Tiny and strongly typed request query validator and parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published