Giter VIP home page Giter VIP logo

typed-qparam's Introduction

typed-qparam

npm-version npm-license npm-download-month npm-min-size ci.yml

🔍 Type-safe query parameter manipulation

Note

See here for documentation on <=v1 features.

Installation

npm i typed-qparam

Simple Usage

Passing a query parameter key to the qparam function returns the accessor for that value.

import { extract } from 'typed-qparam'

const qparam = extract(new URL('https://example.com/?foo=bar'))

const foo = qparam('foo')

// output: 'bar'
console.log(foo.get())

// .set() only returns a new URL instance.
// The original URL instance is not changed.
// No navigation of any kind will occur.

// url: new URL('https://example.com/?foo=baz')
const url = foo.set('baz')

// output: 'https://example.com/?foo=baz'
console.log(url.href)

Typed Param

By passing a conversion function as the second argument, you can obtain a value converted to any type. See ts-serde for more information on type guard.

import { extract } from 'typed-qparam'
import { number } from 'typed-qparam/serde'

const qparam = extract(new URL('https://example.com/?num=123'))

const num = qparam('num', {
  stringify: (value) => value.toString(),
  parse: (str) => parseInt(str)
})

// output 123
console.log(num.get())

// https://example.com/?key=456
const dist = num.set(456)

Prepared Converter

You can also use the prepared converters in typed-qparam/serde. See ts-serde for more information on type guard

import { extract } from 'typed-qparam'
import { number, boolean, enums } from 'typed-qparam/serde'

const qparam = extract(
  new URL('https://example.com/?num=123&bool=true&enumerate=b')
)

const num = qparam('num', number)
const bool = qparam('bool', boolean)
const enumerate = qparam(
  'enumerate',
  enums(
    ['a', 'b', 'c'],
    'a' // fallback default value
  )
)

Array Param

Sometimes you need to handle query parameters with multiple values in the same key, such as ?str=hello&str=world.
With typed-qparam, you can treat this as an array.

import { extract, array } from 'svelte-qparam'
import { string, number } from 'svelte-qparam/serde'

const qparam = extract(new URL('https://example.com/?str=hello&str=world'))

const str = qparam('str', array())
// is equivalent to
// const str = qparam('str', array(string))

// if require other typed value
const num = qparam('num', array(number))

// output ['hello', 'world']
console.log(str.get())

// https://example.com/?str=foo&str=bar&str=baz
str.set(['foo', 'bar', 'baz'])

License

MIT

typed-qparam's People

Contributors

wraith-ci[bot] avatar renovate[bot] avatar jill64 avatar github-actions[bot] avatar

Stargazers

François Best avatar

Watchers

 avatar  avatar

typed-qparam's Issues

Sanctuary

Detected Project Issues

Performance

  • No specific performance issues identified from the provided files.

Reliability

  • Migration to v2 requires changes in custom serializers due to the renaming of properties (stringify and parse to serialize and deserialize). This could potentially break existing implementations if not properly updated.

Maintenance

  • The project relies on external dependencies like ts-serde for serialization and deserialization, which could affect long-term maintenance if these dependencies are not kept up-to-date.

Security

  • No specific security issues identified from the provided files. However, proper handling and validation of query parameters are crucial to prevent injection attacks.

Documentation

  • The README.md file mentions the use of string input for the extract function in the legacy section, which is not supported in v2. This could lead to confusion for users migrating from v1 to v2.
  • The documentation in docs/v1.md and the README.md might not be sufficient for new users to fully understand the migration steps and the changes between versions.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • jill64/.github main
npm
package.json
  • ts-serde 1.0.8
  • @jill64/eslint-config-ts 1.1.42
  • @jill64/prettier-config 1.0.0
  • typescript 5.6.2
  • vitest 2.1.1

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.