Skip to content

Commit 23f2ce9

Browse files
feat: add utils/parse-link-header
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent ffe8061 commit 23f2ce9

11 files changed

Lines changed: 472 additions & 7 deletions

File tree

lib/node_modules/@stdlib/_tools/github/get/lib/resolve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var logger = require( 'debug' );
2424
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25-
var parseHeader = require( './linkheader.js' );
25+
var parseHeader = require( '@stdlib/utils/parse-link-header' );
2626
var request = require( './request.js' );
2727
var flatten = require( './flatten.js' );
2828
var getOptions = require( './options.js' );
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# parseLinkHeader
22+
23+
> Parse a Link header.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var parseLinkHeader = require( '@stdlib/utils/parse-link-header' );
31+
```
32+
33+
#### parseLinkHeader( header )
34+
35+
Parses a `Link` header string.
36+
37+
```javascript
38+
var out = parseLinkHeader( '<https://api.github.com/user/repos?page=2&per_page=1>; rel="next"' );
39+
// returns { 'next': { 'url': 'https://api.github.com/user/repos?page=2&per_page=1', 'page': '2', 'per_page': '1', 'rel': 'next' } }
40+
```
41+
42+
If unable to parse a header, the function returns `null`.
43+
44+
```javascript
45+
var out = parseLinkHeader( 'beep; boop' );
46+
// returns null
47+
```
48+
49+
</section>
50+
51+
<!-- /.usage -->
52+
53+
<section class="notes">
54+
55+
## Notes
56+
57+
- The implementation is intended for pragmatic parsing of pagination `Link` headers and is **not** a standards-complete general-purpose parser.
58+
59+
- The function returns `null` if provided a first argument which is not a `string`.
60+
61+
```javascript
62+
var out = parseLinkHeader( null );
63+
// returns null
64+
```
65+
66+
- The function returns `null` for header values longer than `2000` characters.
67+
68+
</section>
69+
70+
<!-- /.notes -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var parseLinkHeader = require( '@stdlib/utils/parse-link-header' );
80+
81+
var out = parseLinkHeader( '<https://api.github.com/user/repos?page=2&per_page=1>; rel="next", <https://api.github.com/user/repos?page=4&per_page=1>; rel="last"' );
82+
// returns { 'next': {...}, 'last': {...} }
83+
84+
out = parseLinkHeader( '<https://api.github.com/user/repos?page=3>; rel="next prev"; title="next, page"' );
85+
// returns { 'next': {...}, 'prev': {...} }
86+
87+
out = parseLinkHeader( 'beep; boop' );
88+
// returns null
89+
```
90+
91+
</section>
92+
93+
<!-- /.examples -->
94+
95+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
96+
97+
<section class="related">
98+
99+
</section>
100+
101+
<!-- /.related -->
102+
103+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
104+
105+
<section class="links">
106+
107+
</section>
108+
109+
<!-- /.links -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var fromCodePoint = require( '@stdlib/string/from-code-point' );
25+
var pkg = require( './../package.json' ).name;
26+
var parseLinkHeader = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var header;
33+
var out;
34+
var i;
35+
36+
b.tic();
37+
for ( i = 0; i < b.iterations; i++ ) {
38+
header = '<https://api.github.com/user/repos?page=' + (2+(i%8)) + '&per_page=1>; rel="next", ';
39+
header += '<https://api.github.com/user/repos?page=' + (10+(i%8)) + '&per_page=1>; rel="last"; title="page ' + fromCodePoint( 97 + (i%26) ) + '"';
40+
out = parseLinkHeader( header );
41+
if ( out === null ) {
42+
b.fail( 'should return a parsed object' );
43+
}
44+
}
45+
b.toc();
46+
if ( out === null ) {
47+
b.fail( 'should return an object' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( header )
3+
Parses a Link header.
4+
5+
The implementation is intended for pragmatic parsing of pagination Link
6+
headers and is not a standards-complete general-purpose parser.
7+
8+
Header values longer than 2000 characters return `null`.
9+
10+
Parameters
11+
----------
12+
header: string
13+
Header string to parse.
14+
15+
Returns
16+
-------
17+
out: Object|null
18+
Parsed links or null.
19+
20+
Examples
21+
--------
22+
> var header = '<https://api.github.com/user/repos?page=2>; rel="next"';
23+
> var out = {{alias}}( header )
24+
{
25+
... 'next': {
26+
... 'url': 'https://api.github.com/user/repos?page=2',
27+
... 'page': '2',
28+
... 'rel': 'next'
29+
... }
30+
... }
31+
32+
> out = {{alias}}( 'beep; boop' )
33+
null
34+
35+
See Also
36+
--------
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Interface describing a parsed link value.
23+
*/
24+
interface LinkValue {
25+
/**
26+
* Link target URL.
27+
*/
28+
url: string;
29+
30+
/**
31+
* Link relation string.
32+
*/
33+
rel: string;
34+
35+
/**
36+
* Additional parsed link parameters and URL query parameters.
37+
*/
38+
[key: string]: string;
39+
}
40+
41+
/**
42+
* Interface describing parsed link relations.
43+
*/
44+
interface Links {
45+
[key: string]: LinkValue;
46+
}
47+
48+
/**
49+
* Parses a Link header.
50+
*
51+
* @param header - header string to parse
52+
* @returns parsed links or null
53+
*
54+
* @example
55+
* var out = parseLinkHeader( '<https://api.github.com/user/repos?page=2>; rel="next"' );
56+
* // returns { 'next': { 'url': 'https://api.github.com/user/repos?page=2', 'page': '2', 'rel': 'next' } }
57+
*/
58+
declare function parseLinkHeader( header: string ): Links | null;
59+
60+
61+
// EXPORTS //
62+
63+
export = parseLinkHeader;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import parseLinkHeader = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns either parsed links or null...
25+
{
26+
parseLinkHeader( '<https://api.github.com/user/repos?page=2>; rel="next"' ); // $ExpectType Links | null
27+
parseLinkHeader( '' ); // $ExpectType Links | null
28+
}
29+
30+
// The function does not compile if provided a non-string...
31+
{
32+
parseLinkHeader( true ); // $ExpectError
33+
parseLinkHeader( false ); // $ExpectError
34+
parseLinkHeader( 5 ); // $ExpectError
35+
parseLinkHeader( [] ); // $ExpectError
36+
parseLinkHeader( {} ); // $ExpectError
37+
parseLinkHeader( ( x: number ): number => x ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided an unsupported number of arguments...
41+
{
42+
parseLinkHeader(); // $ExpectError
43+
parseLinkHeader( '<https://api.github.com/user/repos?page=2>; rel="next"', 'extra' ); // $ExpectError
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var parseLinkHeader = require( './../lib' );
24+
25+
26+
// MAIN //
27+
28+
var out = parseLinkHeader( '<https://api.github.com/user/repos?page=2&per_page=1>; rel="next", <https://api.github.com/user/repos?page=5&per_page=1>; rel="last"' );
29+
console.log( out );
30+
31+
out = parseLinkHeader( '<https://api.github.com/user/repos?page=3>; rel="next prev"; title="next, page"' );
32+
console.log( out );
33+
34+
out = parseLinkHeader( 'beep; boop' );
35+
console.log( out );
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Parse a Link header.
23+
*
24+
* @module @stdlib/utils/parse-link-header
25+
*
26+
* @example
27+
* var parseLinkHeader = require( '@stdlib/utils/parse-link-header' );
28+
*
29+
* var out = parseLinkHeader( '<https://api.github.com/user/repos?page=2>; rel="next"' );
30+
* // returns { 'next': { 'url': 'https://api.github.com/user/repos?page=2', 'page': '2', 'rel': 'next' } }
31+
*/
32+
33+
// MODULES //
34+
35+
var main = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = main;

0 commit comments

Comments
 (0)