Skip to content

Commit 83519f0

Browse files
authored
Merge branch 'master' into fix(kitsu-core)/meta-object-merge
2 parents 07f8149 + be77507 commit 83519f0

20 files changed

Lines changed: 1128 additions & 871 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454

5555
- name: Upload test coverage to Code Climate
5656
if: ${{ matrix.node_version == 18 }}
57-
uses: paambaati/codeclimate-action@v3.2.0
57+
uses: paambaati/codeclimate-action@v4.0.0
5858
env:
5959
CC_TEST_REPORTER_ID: 7a4b78747587abb295ccb41439d7d067b9de2d885a766e7e88d5e8409599d2ea
6060

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
# [10.1.0](https://github.com/wopian/kitsu/compare/v10.0.5...v10.1.0) (2023-02-28)
7+
8+
9+
### Features
10+
11+
* **kitsu:** Configurable modern query serializer ([ef94ae0](https://github.com/wopian/kitsu/commit/ef94ae0483bbe051f86faf47069168772b6cd5ac))
12+
13+
14+
15+
16+
17+
## [10.0.5](https://github.com/wopian/kitsu/compare/v10.0.4...v10.0.5) (2023-02-28)
18+
19+
20+
### Bug Fixes
21+
22+
* **kitsu-core:** Allow empty POST body ([16cd20d](https://github.com/wopian/kitsu/commit/16cd20d74bf028deaed7a219c590f24a85adc752))
23+
24+
25+
26+
27+
628
## [10.0.4](https://github.com/wopian/kitsu/compare/v10.0.3...v10.0.4) (2022-10-30)
729

830
**Note:** Version bump only for package kitsujs

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "10.0.4",
2+
"version": "10.1.0",
33
"npmClient": "yarn",
44
"useWorkspaces": true,
55
"command": {

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,26 @@
3636
"devDependencies": {
3737
"@babel/cli": "~7.21.0",
3838
"@babel/core": "~7.21.0",
39-
"@babel/preset-env": "~7.20.0",
39+
"@babel/preset-env": "~7.21.0",
4040
"@rollup/plugin-babel": "~6.0.0",
4141
"axios": "~0.27.0",
4242
"axios-mock-adapter": "~1.21.0",
43-
"babel-jest": "~29.4.0",
43+
"babel-jest": "~29.5.0",
4444
"browserslist": "~4.21.0",
4545
"conventional-changelog-angular": "~5.0.12",
4646
"cross-env": "~7.0.0",
4747
"documentation": "~14.0.0",
48-
"eslint": "~8.35.0",
48+
"eslint": "~8.39.0",
4949
"eslint-config-wopian": "~3.0.0",
50-
"jest": "~29.4.0",
50+
"jest": "~29.5.0",
5151
"jest-environment-jsdom": "^29.0.0",
5252
"json-stringify-safe": "~5.0.1",
5353
"lerna": "^3.0.0",
5454
"rollup": "~2.79.0",
5555
"rollup-plugin-delete": "~2.0.0",
5656
"rollup-plugin-local-resolve": "~1.0.7",
5757
"size-limit": "~8.2.0",
58-
"typescript": "~4.9.0"
58+
"typescript": "~5.0.0"
5959
},
6060
"jest": {
6161
"coverageThreshold": {

packages/kitsu-core/CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
# [10.1.0](https://github.com/wopian/kitsu/compare/v10.0.5...v10.1.0) (2023-02-28)
7+
8+
9+
### Features
10+
11+
* **kitsu:** Configurable modern query serializer ([ef94ae0](https://github.com/wopian/kitsu/commit/ef94ae0483bbe051f86faf47069168772b6cd5ac))
12+
13+
14+
15+
16+
17+
## [10.0.5](https://github.com/wopian/kitsu/compare/v10.0.4...v10.0.5) (2023-02-28)
18+
19+
20+
### Bug Fixes
21+
22+
* **kitsu-core:** Allow empty POST body ([16cd20d](https://github.com/wopian/kitsu/commit/16cd20d74bf028deaed7a219c590f24a85adc752))
23+
24+
25+
26+
27+
628
## [10.0.4](https://github.com/wopian/kitsu/compare/v10.0.3...v10.0.4) (2022-10-30)
729

830
**Note:** Version bump only for package kitsu-core

packages/kitsu-core/README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ All code released under [MIT]
137137

138138
### camel
139139

140-
[packages/kitsu-core/src/camel/index.js:14-14](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/camel/index.js#L14-L14 "Source code on GitHub")
140+
[packages/kitsu-core/src/camel/index.js:14-14](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/camel/index.js#L14-L14 "Source code on GitHub")
141141

142142
Converts kebab-case and snake\_case into camelCase
143143

@@ -163,7 +163,7 @@ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
163163

164164
### deattribute
165165

166-
[packages/kitsu-core/src/deattribute/index.js:29-51](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/deattribute/index.js#L29-L51 "Source code on GitHub")
166+
[packages/kitsu-core/src/deattribute/index.js:29-51](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/deattribute/index.js#L29-L51 "Source code on GitHub")
167167

168168
Hoists attributes to be top-level
169169

@@ -205,7 +205,7 @@ Returns **([Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/
205205

206206
### deserialise
207207

208-
[packages/kitsu-core/src/deserialise/index.js:62-77](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/deserialise/index.js#L62-L77 "Source code on GitHub")
208+
[packages/kitsu-core/src/deserialise/index.js:62-77](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/deserialise/index.js#L62-L77 "Source code on GitHub")
209209

210210
Deserialises a JSON-API response
211211

@@ -255,7 +255,7 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
255255

256256
### error
257257

258-
[packages/kitsu-core/src/error/index.js:27-33](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/error/index.js#L27-L33 "Source code on GitHub")
258+
[packages/kitsu-core/src/error/index.js:27-33](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/error/index.js#L27-L33 "Source code on GitHub")
259259

260260
Uniform error handling for Axios, JSON:API and internal package errors. Mutated Error object is rethrown to the caller.
261261

@@ -292,7 +292,7 @@ error({
292292

293293
### filterIncludes
294294

295-
[packages/kitsu-core/src/filterIncludes/index.js:33-46](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/filterIncludes/index.js#L33-L46 "Source code on GitHub")
295+
[packages/kitsu-core/src/filterIncludes/index.js:33-46](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/filterIncludes/index.js#L33-L46 "Source code on GitHub")
296296

297297
Filters includes for the specific relationship requested
298298

@@ -332,7 +332,7 @@ Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Gl
332332

333333
### kebab
334334

335-
[packages/kitsu-core/src/kebab/index.js:11-11](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/kebab/index.js#L11-L11 "Source code on GitHub")
335+
[packages/kitsu-core/src/kebab/index.js:11-11](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/kebab/index.js#L11-L11 "Source code on GitHub")
336336

337337
Converts camelCase into kebab-case
338338

@@ -350,7 +350,7 @@ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
350350

351351
### linkRelationships
352352

353-
[packages/kitsu-core/src/linkRelationships/index.js:144-164](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/linkRelationships/index.js#L144-L164 "Source code on GitHub")
353+
[packages/kitsu-core/src/linkRelationships/index.js:144-164](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/linkRelationships/index.js#L144-L164 "Source code on GitHub")
354354

355355
Links relationships to included data
356356

@@ -390,7 +390,7 @@ Returns **any** Parsed data
390390

391391
### isDeepEqual
392392

393-
[packages/kitsu-core/src/deepEqual/index.js:18-42](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/deepEqual/index.js#L18-L42 "Source code on GitHub")
393+
[packages/kitsu-core/src/deepEqual/index.js:18-42](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/deepEqual/index.js#L18-L42 "Source code on GitHub")
394394

395395
Compare two objects equality
396396

@@ -419,14 +419,15 @@ Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/
419419

420420
### query
421421

422-
[packages/kitsu-core/src/query/index.js:34-43](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/query/index.js#L34-L43 "Source code on GitHub")
422+
[packages/kitsu-core/src/query/index.js:55-64](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/query/index.js#L55-L64 "Source code on GitHub")
423423

424424
Constructs a URL query string for JSON:API parameters
425425

426426
#### Parameters
427427

428428
* `params` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Parameters to parse
429429
* `prefix` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** Prefix for nested parameters - used internally (optional, default `null`)
430+
* `traditional` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Use the traditional (default) or modern param serializer. Set to false if your server is running Ruby on Rails or other modern web frameworks (optional, default `true`)
430431

431432
#### Examples
432433

@@ -447,7 +448,7 @@ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
447448

448449
### serialise
449450

450-
[packages/kitsu-core/src/serialise/index.js:213-224](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/serialise/index.js#L213-L224 "Source code on GitHub")
451+
[packages/kitsu-core/src/serialise/index.js:213-224](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/serialise/index.js#L213-L224 "Source code on GitHub")
451452

452453
Serialises an object into a JSON-API structure
453454

@@ -492,7 +493,7 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
492493

493494
### snake
494495

495-
[packages/kitsu-core/src/snake/index.js:11-11](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/snake/index.js#L11-L11 "Source code on GitHub")
496+
[packages/kitsu-core/src/snake/index.js:11-11](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/snake/index.js#L11-L11 "Source code on GitHub")
496497

497498
Converts camelCase into snake\_case
498499

@@ -510,7 +511,7 @@ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
510511

511512
### splitModel
512513

513-
[packages/kitsu-core/src/splitModel/index.js:29-39](https://github.com/wopian/kitsu/blob/55f31c666be1b67ae2eb7d5d16fecd181c0028db/packages/kitsu-core/src/splitModel/index.js#L29-L39 "Source code on GitHub")
514+
[packages/kitsu-core/src/splitModel/index.js:29-39](https://github.com/wopian/kitsu/blob/feedef8ad5594ee55351dda1136c097c8667a4cd/packages/kitsu-core/src/splitModel/index.js#L29-L39 "Source code on GitHub")
514515

515516
Split model name from the model's resource URL
516517

packages/kitsu-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "10.0.4",
2+
"version": "10.1.0",
33
"name": "kitsu-core",
44
"description": "Simple, lightweight & framework agnostic JSON:API (de)serialsation components",
55
"author": "James Harris <wopian@wopian.me>",

packages/kitsu-core/src/query/index.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,40 @@
33
*
44
* @param {string} value Right-hand side of the query
55
* @param {string} key Left-hand side of the query
6+
* @param {boolean} traditional use traditional array key serializer
7+
*
68
* @returns {string} URL query string
79
* @private
810
*/
9-
function queryFormat (value, key) {
10-
if (value !== null && Array.isArray(value)) return value.map(v => queryFormat(v, key)).join('&')
11-
else if (value !== null && typeof value === 'object') return query(value, key)
11+
function queryFormat (value, key, traditional) {
12+
if (traditional && value !== null && Array.isArray(value)) return value.map(v => queryFormat(v, key, traditional)).join('&')
13+
if (!traditional && value !== null && Array.isArray(value)) return value.map(v => queryFormat(v, `${key}[]`, traditional)).join('&')
14+
else if (value !== null && typeof value === 'object') return query(value, key, traditional)
1215
else return encodeURIComponent(key) + '=' + encodeURIComponent(value)
1316
}
1417

18+
/**
19+
* Formats key names to correct array syntax
20+
*
21+
* @param {string} [param] Parameter name to parse
22+
*
23+
* @returns {string} Key name in nested query-param format with optional array style suffix
24+
* @private
25+
*/
26+
export function paramKeyName (param) {
27+
if ([ '[]', '][' ].includes(param.slice(-2))) {
28+
return `[${param.slice(0, -2)}][]`
29+
}
30+
31+
return `[${param}]`
32+
}
33+
1534
/**
1635
* Constructs a URL query string for JSON:API parameters
1736
*
1837
* @param {Object} [params] Parameters to parse
1938
* @param {string} [prefix] Prefix for nested parameters - used internally
39+
* @param {boolean} [traditional=true] Use the traditional (default) or modern param serializer. Set to false if your server is running Ruby on Rails or other modern web frameworks
2040
* @returns {string} URL query string
2141
*
2242
* @example
@@ -31,12 +51,13 @@ function queryFormat (value, key) {
3151
* })
3252
* // filter%5Bslug%5D=cowboy-bebop&filter%5Btitle%5D%5Bvalue%5D=foo&sort=-id
3353
*/
34-
export function query (params, prefix = null) {
54+
55+
export function query (params, prefix = null, traditional = true) {
3556
const str = []
3657

3758
for (const param in params) {
3859
str.push(
39-
queryFormat(params[param], prefix ? `${prefix}[${param}]` : param)
60+
queryFormat(params[param], prefix ? `${prefix}${paramKeyName(param)}` : param, traditional)
4061
)
4162
}
4263
return str.join('&')

packages/kitsu-core/src/query/index.spec.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('kitsu-core', () => {
5959
})).toEqual('fields%5Babc%5D%5Bdef%5D%5Bghi%5D%5Bjkl%5D=mno')
6060
})
6161

62-
it('builds list parameters', () => {
62+
it('builds list parameters in traditional mode', () => {
6363
expect.assertions(1)
6464
expect(query({
6565
filter: {
@@ -68,13 +68,50 @@ describe('kitsu-core', () => {
6868
})).toEqual('filter%5Bid_in%5D=1&filter%5Bid_in%5D=2&filter%5Bid_in%5D=3')
6969
})
7070

71-
it('builds nested list parameters', () => {
71+
it('builds nested list parameters in traditional mode', () => {
7272
expect.assertions(1)
7373
expect(query({
7474
filter: {
7575
users: [ { id: 1, type: 'users' }, { id: 2, type: 'users' } ]
7676
}
7777
})).toEqual('filter%5Busers%5D%5Bid%5D=1&filter%5Busers%5D%5Btype%5D=users&filter%5Busers%5D%5Bid%5D=2&filter%5Busers%5D%5Btype%5D=users')
7878
})
79+
80+
it('builds list parameters in modern mode', () => {
81+
expect.assertions(1)
82+
expect(query({
83+
filter: {
84+
id_in: [ 1, 2, 3 ]
85+
}
86+
}, null, false)).toEqual('filter%5Bid_in%5D%5B%5D=1&filter%5Bid_in%5D%5B%5D=2&filter%5Bid_in%5D%5B%5D=3')
87+
})
88+
89+
it('builds nested list parameters in modern mode', () => {
90+
expect.assertions(1)
91+
expect(query({
92+
filter: {
93+
users: [ { id: 1, type: 'users' }, { id: 2, type: 'users' } ]
94+
}
95+
}, null, false)).toEqual('filter%5Busers%5D%5B%5D%5Bid%5D=1&filter%5Busers%5D%5B%5D%5Btype%5D=users&filter%5Busers%5D%5B%5D%5Bid%5D=2&filter%5Busers%5D%5B%5D%5Btype%5D=users')
96+
})
97+
98+
it('parses list-style keys', () => {
99+
expect.assertions(1)
100+
expect(query({
101+
filter: {
102+
'id_in[]': [ 1, 2 ],
103+
'parent_id_in][': [ 3, 4 ]
104+
}
105+
})).toEqual('filter%5Bid_in%5D%5B%5D=1&filter%5Bid_in%5D%5B%5D=2&filter%5Bparent_id_in%5D%5B%5D=3&filter%5Bparent_id_in%5D%5B%5D=4')
106+
})
107+
108+
it('preserves square-brackets in key names in modern mode', () => {
109+
expect.assertions(1)
110+
expect(query({
111+
filter: {
112+
'id_in[]': [ 1, 2 ]
113+
}
114+
}, null, false)).toEqual('filter%5Bid_in%5D%5B%5D%5B%5D=1&filter%5Bid_in%5D%5B%5D%5B%5D=2')
115+
})
79116
})
80117
})

packages/kitsu-core/src/serialise/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function isValid (isArray, type, payload, method) {
2424
}
2525
}
2626
} else {
27-
if (typeof payload !== 'object' || Object.keys(payload).length === 0) {
27+
if (typeof payload !== 'object' || (method !== 'POST' && Object.keys(payload).length === 0)) {
2828
throw new Error(`${method} requires an object or array body`)
2929
}
3030
// A POST request is the only request to not require an ID in spec

0 commit comments

Comments
 (0)