Utility for parsing URL query parameters with types.
npm install urlargsType-safe utility for parsing URL query parameters.
Data Types:
- Booleans
- Arrays
- Typed Arrays
- Nullish Types
- Allowed Types
- JSON Types
``ts
import { UrlArgs } from 'urlargs';
// Define default values
const args = new UrlArgs( {
enabled: false,
count: 10,
name: 'test',
tags: [ 'a', 'b' ],
} );
// š Query: ?count=20&enabled=true&name=foo
// Get typed parameters based on the defaults
const { count, enabled, name, tags } = args.values;
count ā 20
enabled ā true
name ā 'foo'
tags ā [ 'a', 'b' ]
`
Generate a table of the parameters and their descriptions in the console with describe():
`ts`
args.describe( {
count: 'The number of items to display',
enabled: 'Whether the items are enabled',
name: 'The name of the items',
} );
Values that differ from the defaults will be highlighted.
Whenever a parameter is invalid, a console warning will be shown and the default value will be used.
These are the valid values for booleans.
| True | False |
|------|-------|
| š¢ ?enabled=true | š“ ?enabled=false |?enabled=TRUE
| š¢ | š“ ?enabled=FALSE |?enabled=1
| š¢ | š“ ?enabled=0 |?enabled
| š¢ | | ?enabled=
| š¢ | |
By default, parameters that appear multiple times are collected into a string array.
š Query: ?tags=a&tags=b`ts`
const args = new UrlArgs( { tags: [] } );
args.values.tags ā ['a', 'b']
By default, arrays use repeated mode. You can optionally enable comma mode to allow comma-separated arrays:
š Query: ?tags=a,b,c`ts
UrlArgs.arrayMode = 'comma';
const args = new UrlArgs( { tags: [] } );
args.values.tags ā ['a', 'b', 'c']
`
In comma mode, you can escape commas with a backslash: ?tags=a,b\,c ā ['a', 'b,c']
By default, arrays are assumed to be string[]. To specify a different type, use the $array type.
š Query: ?numbers=1,2,3&booleans=true,false,true`ts
import { UrlArgs, $array } from 'urlargs';
UrlArgs.arrayMode = 'comma';
const args = new UrlArgs( {
numbers: $array.number,
booleans: $array.boolean,
} );
args.values.numbers ā [ 1, 2, 3 ]
args.values.booleans ā [ true, false, true ]
`
To restrict a parameter to a specific set of allowed values, use the $allowed type.
`ts
import { UrlArgs, $allowed } from 'urlargs';
const args = new UrlArgs( {
theme: $allowed.string( 'light', 'dark', 'auto' ),
fontSize: $allowed.number( 12, 14, 16, 18 ),
} );
`$allowed arguments will become a union type of the allowed values. They default to the first value. The list of allowed values will be displayed by describe().
For arguments that can be undefined or null, use the $undefined or $null types.
š Query: ?count=2`ts
import { UrlArgs, $undefined, $null } from 'urlargs';
const args = new UrlArgs( {
count: $undefined.number,
description: $null.string,
} );
args.values.count ā 2
args.values.description ā null
`
Nullish types can have non-nullish defaults, which can then be overridden by the URL.
š Query: ?count=undefined`ts
const args = new UrlArgs( {
count: $undefined.number( 100 ),
description: $null.string,
} );
args.values.count ā undefined
args.values.description ā null
`
| undefined|null|
|---------------------|-----------------|
| $undefined.number | $null.number |$undefined.boolean
| | $null.boolean |$undefined.string
| | $null.string |
To parse JSON values from URL parameters, use the $json type.
š Query: ?config={ "w": 200, "h": 300, "info": { "on": true } }&items=[ 4, 5, 6 ]
`ts
import { UrlArgs, $json } from 'urlargs';
const args = new UrlArgs( {
config: $json( {
w: 100,
h: 100,
info: { on: false }
} ),
items: $json( [ 1, 2, 3 ] ),
} );
args.values.items ā [ 4, 5, 6 ]
args.values.config ā { w: 200, h: 300, info: { on: true } }
`
$json types must specify a default value. They will inherit the type of the default value.
ā ļø There is no schema validation with $json`. The only check is that the value is valid JSON.