Skip to content

Commit f557cee

Browse files
authored
Backport request, request-utils, store cache-handler changes to 4.12 (#9389)
* Backport @ember-data/request changes from main * Backport cache-handler changes from main * Backport fetch handler changes from main * Backport @ember-data/request-utils * Don't build types for @ember-data/request-utils * Downgrade @ember-data/request-utils node engines * Fix @ember-data/request-utils build * Fix lint * Fix problems * Fix special-build-tests * Remove request-utils changelog
1 parent 9779c67 commit f557cee

33 files changed

+5493
-1006
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ module.exports = {
321321
'packages/*/blueprints/*/index.js',
322322
'packages/*/config/**/*.js',
323323
'packages/*/tests/dummy/config/**/*.js',
324+
'packages/request-utils/rollup/external.cjs',
324325
],
325326
excludedFiles: [
326327
'packages/*/addon/**',

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dist-*
99
tmp
1010
packages/tracking/addon
1111
packages/request/addon
12+
packages/request-utils/addon
1213
packages/store/addon
1314
packages/adapter/addon
1415
packages/serializer/addon

packages/json-api/src/-private/cache.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import type { LocalRelationshipOperation } from '@ember-data/graph/-private/grap
1111
import type { ImplicitRelationship } from '@ember-data/graph/-private/graph/index';
1212
import type BelongsToRelationship from '@ember-data/graph/-private/relationships/state/belongs-to';
1313
import type ManyRelationship from '@ember-data/graph/-private/relationships/state/has-many';
14-
import { StructuredErrorDocument } from '@ember-data/request/-private/types';
15-
import { StoreRequestInfo } from '@ember-data/store/-private/cache-handler';
14+
import { type ImmutableRequestInfo, StructuredErrorDocument } from '@ember-data/request/-private/types';
1615
import type { IdentifierCache } from '@ember-data/store/-private/caches/identifier-cache';
1716
import type { ResourceBlob } from '@ember-data/types/cache/aliases';
1817
import type { Change } from '@ember-data/types/cache/change';
@@ -236,7 +235,7 @@ export default class JSONAPICache implements Cache {
236235
(resourceDocument as SingleResourceDataDocument | CollectionResourceDataDocument).data = data;
237236
}
238237

239-
const request = doc.request as StoreRequestInfo | undefined;
238+
const request = doc.request as ImmutableRequestInfo | undefined;
240239
const identifier = request ? this.__storeWrapper.identifierCache.getOrCreateDocumentIdentifier(request) : null;
241240

242241
if (identifier) {

packages/legacy-compat/src/builders/find-all.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
*/
44
import { assert } from '@ember/debug';
55

6-
import type Model from '@ember-data/model';
76
import { SkipCache } from '@ember-data/request';
87
import type { ImmutableRequestInfo } from '@ember-data/request/-private/types';
8+
99
import { normalizeModelName } from './utils';
1010

1111
// Keeping unused generics for consistency with 5x types
12-
type FindAllRequestInput<T extends string = string, RT = unknown[]> = ImmutableRequestInfo & {
12+
type FindAllRequestInput<T extends string = string> = ImmutableRequestInfo & {
1313
op: 'findAll';
1414
data: {
1515
type: T;
@@ -43,11 +43,7 @@ type FindAllBuilderOptions = {
4343
@param {FindAllBuilderOptions} [options] optional, may include `adapterOptions` hash which will be passed to adapter.findAll
4444
@return {FindAllRequestInput} request config
4545
*/
46-
// Keeping this generic for consistency with 5x types
47-
export function findAllBuilder<T extends Model>(
48-
type: string,
49-
options?: FindAllBuilderOptions
50-
): FindAllRequestInput<string, Model[]>;
46+
export function findAllBuilder(type: string, options?: FindAllBuilderOptions): FindAllRequestInput<string>;
5147
export function findAllBuilder(type: string, options?: FindAllBuilderOptions): FindAllRequestInput;
5248
export function findAllBuilder(type: string, options: FindAllBuilderOptions = {}): FindAllRequestInput {
5349
assert(`You need to pass a model name to the findAll builder`, type);

packages/legacy-compat/src/builders/find-record.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
*/
44
import { assert } from '@ember/debug';
55

6-
import type Model from '@ember-data/model';
76
import { SkipCache } from '@ember-data/request';
87
import type { ImmutableRequestInfo } from '@ember-data/request/-private/types';
98
import { constructResource, ensureStringId } from '@ember-data/store/-private';
109
import type { ResourceIdentifierObject } from '@ember-data/types/q/ember-data-json-api';
10+
1111
import { isMaybeIdentifier, normalizeModelName } from './utils';
1212

13-
// Keeping unused generics for consistency with 5x types
14-
type FindRecordRequestInput<T extends string = string, RT = unknown> = ImmutableRequestInfo & {
13+
type FindRecordRequestInput = ImmutableRequestInfo & {
1514
op: 'findRecord';
1615
data: {
1716
record: ResourceIdentifierObject;
@@ -63,20 +62,20 @@ type FindRecordBuilderOptions = {
6362
@param {FindRecordBuilderOptions} [options] - if the first param is a string this will be the optional options for the request. See examples for available options.
6463
@return {FindRecordRequestInput} request config
6564
*/
66-
export function findRecordBuilder<T extends Model>(
65+
export function findRecordBuilder(
6766
resource: string,
6867
id: string,
6968
options?: FindRecordBuilderOptions
70-
): FindRecordRequestInput<string, T>;
69+
): FindRecordRequestInput;
7170
export function findRecordBuilder(
7271
resource: string,
7372
id: string,
7473
options?: FindRecordBuilderOptions
7574
): FindRecordRequestInput;
76-
export function findRecordBuilder<T extends Model>(
75+
export function findRecordBuilder(
7776
resource: ResourceIdentifierObject,
7877
options?: FindRecordBuilderOptions
79-
): FindRecordRequestInput<string, T>;
78+
): FindRecordRequestInput;
8079
export function findRecordBuilder(
8180
resource: ResourceIdentifierObject,
8281
options?: FindRecordBuilderOptions
@@ -104,7 +103,7 @@ export function findRecordBuilder(
104103

105104
options = options || {};
106105

107-
assert('findRecord builder does not support options.preload', !(options as any).preload);
106+
assert('findRecord builder does not support options.preload', !(options as { preload?: boolean }).preload);
108107

109108
return {
110109
op: 'findRecord' as const,

packages/legacy-compat/src/builders/query.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
*/
44
import { assert } from '@ember/debug';
55

6-
import type Model from '@ember-data/model';
76
import { SkipCache } from '@ember-data/request';
87
import type { ImmutableRequestInfo } from '@ember-data/request/-private/types';
8+
99
import { normalizeModelName } from './utils';
1010

11-
type QueryRequestInput<T extends string = string, RT = unknown[]> = ImmutableRequestInfo & {
11+
type QueryRequestInput<T extends string = string> = ImmutableRequestInfo & {
1212
op: 'query';
1313
data: {
1414
type: T;
@@ -40,11 +40,11 @@ type QueryBuilderOptions = {
4040
@param {QueryBuilderOptions} [options] optional, may include `adapterOptions` hash which will be passed to adapter.query
4141
@return {QueryRequestInput} request config
4242
*/
43-
export function queryBuilder<T extends Model>(
43+
export function queryBuilder(
4444
type: string,
4545
query: Record<string, unknown>,
4646
options?: QueryBuilderOptions
47-
): QueryRequestInput<string, T[]>;
47+
): QueryRequestInput<string>;
4848
export function queryBuilder(
4949
type: string,
5050
query: Record<string, unknown>,
@@ -73,7 +73,7 @@ export function queryBuilder(
7373
};
7474
}
7575

76-
type QueryRecordRequestInput<T extends string = string, RT = unknown> = ImmutableRequestInfo & {
76+
type QueryRecordRequestInput<T extends string = string> = ImmutableRequestInfo & {
7777
op: 'queryRecord';
7878
data: {
7979
type: T;
@@ -101,11 +101,11 @@ type QueryRecordRequestInput<T extends string = string, RT = unknown> = Immutabl
101101
@param {QueryBuilderOptions} [options] optional, may include `adapterOptions` hash which will be passed to adapter.query
102102
@return {QueryRecordRequestInput} request config
103103
*/
104-
export function queryRecordBuilder<T extends Model>(
104+
export function queryRecordBuilder(
105105
type: string,
106106
query: Record<string, unknown>,
107107
options?: QueryBuilderOptions
108-
): QueryRecordRequestInput<string, T | null>;
108+
): QueryRecordRequestInput<string>;
109109
export function queryRecordBuilder(
110110
type: string,
111111
query: Record<string, unknown>,

packages/legacy-compat/src/builders/save-record.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { InstanceCache } from '@ember-data/store/-private/caches/instance-c
1111
import type { Cache } from '@ember-data/types/cache/cache';
1212
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
1313

14-
type SaveRecordRequestInput<T extends string = string, RT = unknown> = ImmutableRequestInfo & {
14+
type SaveRecordRequestInput = ImmutableRequestInfo & {
1515
op: 'createRecord' | 'deleteRecord' | 'updateRecord';
1616
data: {
1717
record: StableRecordIdentifier;
@@ -52,7 +52,7 @@ function resourceIsFullyDeleted(instanceCache: InstanceCache, identifier: Stable
5252
export function saveRecordBuilder<T extends Model>(
5353
record: T,
5454
options: Record<string, unknown> = {}
55-
): SaveRecordRequestInput<string, T> {
55+
): SaveRecordRequestInput {
5656
const store = storeFor(record);
5757
assert(`Unable to initiate save for a record in a disconnected state`, store);
5858
const identifier = recordIdentifierFor(record);

packages/legacy-compat/src/builders/utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { ResourceIdentifierObject } from '@ember-data/types/q/ember-data-json-api';
21
import { dasherize } from '@ember/string';
32

3+
import type { ResourceIdentifierObject } from '@ember-data/types/q/ember-data-json-api';
4+
45
export function isMaybeIdentifier(
56
maybeIdentifier: string | ResourceIdentifierObject
67
): maybeIdentifier is ResourceIdentifierObject {

packages/legacy-compat/src/legacy-network-handler/fetch-manager.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import { DEPRECATE_RSVP_PROMISE, DEPRECATE_V1_RECORD_DATA } from '@ember-data/de
66
import { DEBUG, TESTING } from '@ember-data/env';
77
import { HAS_GRAPH_PACKAGE } from '@ember-data/packages';
88
import { createDeferred } from '@ember-data/request';
9-
import type { Deferred } from '@ember-data/request/-private/types';
9+
import type { Deferred, ImmutableRequestInfo } from '@ember-data/request/-private/types';
1010
import type Store from '@ember-data/store';
1111
import { coerceId } from '@ember-data/store/-private';
12-
import { StoreRequestInfo } from '@ember-data/store/-private/cache-handler';
1312
import type { InstanceCache } from '@ember-data/store/-private/caches/instance-cache';
1413
import type ShimModelClass from '@ember-data/store/-private/legacy-model-support/shim-model-class';
1514
import type RequestStateService from '@ember-data/store/-private/network/request-cache';
@@ -115,7 +114,7 @@ export default class FetchManager {
115114
scheduleFetch(
116115
identifier: StableExistingRecordIdentifier,
117116
options: FindOptions,
118-
request: StoreRequestInfo
117+
request: ImmutableRequestInfo
119118
): Promise<StableExistingRecordIdentifier> {
120119
let query: FindRecordQuery = {
121120
op: 'findRecord',
@@ -251,7 +250,7 @@ export default class FetchManager {
251250
fetchDataIfNeededForIdentifier(
252251
identifier: StableExistingRecordIdentifier,
253252
options: FindOptions = {},
254-
request: StoreRequestInfo
253+
request: ImmutableRequestInfo
255254
): Promise<StableExistingRecordIdentifier> {
256255
// pre-loading will change the isEmpty value
257256
const isEmpty = _isEmpty(this._store._instanceCache, identifier);

packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { importSync } from '@embroider/macros';
55
import { LOG_PAYLOADS } from '@ember-data/debugging';
66
import { DEPRECATE_V1_RECORD_DATA } from '@ember-data/deprecations';
77
import { DEBUG, TESTING } from '@ember-data/env';
8-
import type { Handler, NextFn } from '@ember-data/request/-private/types';
8+
import type { Handler, ImmutableRequestInfo, NextFn } from '@ember-data/request/-private/types';
99
import type Store from '@ember-data/store';
10-
import type { StoreRequestContext, StoreRequestInfo } from '@ember-data/store/-private/cache-handler';
10+
import type { StoreRequestContext } from '@ember-data/store/-private/cache-handler';
1111
import type { Collection } from '@ember-data/store/-private/record-arrays/identifier-array';
1212
import { SingleResourceDataDocument } from '@ember-data/types/cache/document';
1313
import type { ModelSchema } from '@ember-data/types/q/ds-model';
@@ -419,7 +419,7 @@ function _findAll<T>(
419419
store: Store,
420420
type: string,
421421
snapshotArray: SnapshotRecordArray,
422-
request: StoreRequestInfo,
422+
request: ImmutableRequestInfo,
423423
isAsyncFlush: boolean
424424
): Promise<T> {
425425
const schema = store.modelFor(type);

packages/request-utils/LICENSE.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The MIT License (MIT)
2+
3+
Copyright (C) 2017-2023 Ember.js contributors
4+
Portions Copyright (C) 2011-2017 Tilde, Inc. and contributors.
5+
Portions Copyright (C) 2011 LivingSocial Inc.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/request-utils/README.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<p align="center">
2+
<img
3+
class="project-logo"
4+
src="./ember-data-logo-dark.svg#gh-dark-mode-only"
5+
alt="EmberData RequestUtils"
6+
width="240px"
7+
title="EmberData RequestUtils"
8+
/>
9+
<img
10+
class="project-logo"
11+
src="./ember-data-logo-light.svg#gh-light-mode-only"
12+
alt="EmberData RequestUtils"
13+
width="240px"
14+
title="EmberData RequestUtils"
15+
/>
16+
</p>
17+
18+
<p align="center">Utilities for Requests</p>
19+
20+
This package provides Simple utility function to assist in url building, query params, and other common request operations.
21+
22+
It's built for [*Ember***Data**](https://github.com/emberjs/data/) but useful more broadly if you're looking for lightweight functions to assist in working with urls and query params.
23+
24+
## Installation
25+
26+
Install using your javascript package manager of choice. For instance with [pnpm](https://pnpm.io/)
27+
28+
```no-highlight
29+
pnpm add @ember-data/request-utils
30+
```
31+
32+
**Tagged Releases**
33+
34+
- ![NPM Canary Version](https://img.shields.io/npm/v/%40ember-data/request-utils/canary?label=%40canary&color=FFBF00)
35+
- ![NPM Beta Version](https://img.shields.io/npm/v/%40ember-data/request-utils/beta?label=%40beta&color=ff00ff)
36+
- ![NPM Stable Version](https://img.shields.io/npm/v/%40ember-data/request-utils/latest?label=%40latest&color=90EE90)
37+
- ![NPM LTS Version](https://img.shields.io/npm/v/%40ember-data/request-utils/lts?label=%40lts&color=0096FF)
38+
- ![NPM LTS 4.12 Version](https://img.shields.io/npm/v/%40ember-data/request-utils/lts-4-12?label=%40lts-4-12&color=bbbbbb)
39+
40+
41+
## Utils
42+
43+
- [buildBaseUrl]()
44+
- [sortQueryParams]()
45+
- [buildQueryParams]()
46+
- [filterEmpty]()
47+
48+
### As a Library Primitive
49+
50+
These primitives may be used directly or composed by request builders to provide a consistent interface for building requests.
51+
52+
For instance:
53+
54+
```ts
55+
import { buildBaseURL, buildQueryParams } from '@ember-data/request-utils';
56+
57+
const baseURL = buildBaseURL({
58+
host: 'https://api.example.com',
59+
namespace: 'api/v1',
60+
resourcePath: 'emberDevelopers',
61+
op: 'query',
62+
identifier: { type: 'ember-developer' }
63+
});
64+
const url = `${baseURL}?${buildQueryParams({ name: 'Chris', include:['pets'] })}`;
65+
// => 'https://api.example.com/api/v1/emberDevelopers?include=pets&name=Chris'
66+
```
67+
68+
This is useful, but not as useful as the REST request builder for query which is sugar over this (and more!):
69+
70+
```ts
71+
import { query } from '@ember-data/rest/request';
72+
73+
const options = query('ember-developer', { name: 'Chris', include:['pets'] });
74+
// => { url: 'https://api.example.com/api/v1/emberDevelopers?include=pets&name=Chris' }
75+
// Note: options will also include other request options like headers, method, etc.
76+
```

packages/request-utils/addon-main.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
name: require('./package.json').name,
3+
4+
treeForVendor() {
5+
return;
6+
},
7+
treeForPublic() {
8+
return;
9+
},
10+
treeForStyles() {
11+
return;
12+
},
13+
treeForAddonStyles() {
14+
return;
15+
},
16+
treeForApp() {
17+
return;
18+
},
19+
};
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"plugins": [
3+
"@babel/plugin-transform-runtime",
4+
["@babel/plugin-transform-typescript", { "allowDeclareFields": true }],
5+
["@babel/plugin-proposal-decorators", { "legacy": true }],
6+
"@babel/plugin-transform-class-properties"
7+
]
8+
}

0 commit comments

Comments
 (0)