Skip to content

Commit 22a8f20

Browse files
snewcomerIgor TerzicScott Newcomer
authored
[Chore]: TypeScript adapters (#7358)
* [Chore]: ts adapters Co-authored-by: Igor Terzic <[email protected]> Co-authored-by: Scott Newcomer <[email protected]>
1 parent 5c661db commit 22a8f20

File tree

9 files changed

+347
-150
lines changed

9 files changed

+347
-150
lines changed

packages/-ember-data/tests/integration/identifiers/scenarios-test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module('Integration | Identifiers - scenarios', function(hooks) {
3131
module('Secondary Cache based on an attribute', function(hooks) {
3232
let store;
3333
let calls;
34+
let isQuery = false;
3435
let secondaryCache: {
3536
id: ConfidentDict<string>;
3637
username: ConfidentDict<string>;
@@ -44,10 +45,11 @@ module('Integration | Identifiers - scenarios', function(hooks) {
4445
shouldBackgroundReloadRecord() {
4546
return false;
4647
}
47-
findRecord(isQuery = false) {
48+
findRecord() {
4849
if (isQuery !== true) {
4950
calls.findRecord++;
5051
}
52+
isQuery = false;
5153
return resolve({
5254
data: {
5355
id: '1',
@@ -62,7 +64,8 @@ module('Integration | Identifiers - scenarios', function(hooks) {
6264
}
6365
queryRecord() {
6466
calls.queryRecord++;
65-
return this.findRecord(true);
67+
isQuery = true;
68+
return this.findRecord();
6669
}
6770
}
6871

@@ -234,6 +237,7 @@ module('Integration | Identifiers - scenarios', function(hooks) {
234237
module('Secondary Cache using an attribute as an alternate id', function(hooks) {
235238
let store;
236239
let calls;
240+
let isQuery = false;
237241
let secondaryCache: ConfidentDict<string>;
238242
class TestSerializer extends Serializer {
239243
normalizeResponse(_, __, payload) {
@@ -244,10 +248,11 @@ module('Integration | Identifiers - scenarios', function(hooks) {
244248
shouldBackgroundReloadRecord() {
245249
return false;
246250
}
247-
findRecord(isQuery = false) {
251+
findRecord() {
248252
if (isQuery !== true) {
249253
calls.findRecord++;
250254
}
255+
isQuery = false;
251256
return resolve({
252257
data: {
253258
id: '1',
@@ -262,7 +267,8 @@ module('Integration | Identifiers - scenarios', function(hooks) {
262267
}
263268
queryRecord() {
264269
calls.queryRecord++;
265-
return this.findRecord(true);
270+
isQuery = true;
271+
return this.findRecord();
266272
}
267273
}
268274

packages/adapter/addon/-private/build-url-mixin.js renamed to packages/adapter/addon/-private/build-url-mixin.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { camelize } from '@ember/string';
44

55
import { pluralize } from 'ember-inflector';
66

7+
type Dict<T> = import('@ember-data/store/-private/ts-interfaces/utils').Dict<T>;
8+
type Snapshot = import('@ember-data/store/-private/system/snapshot').default;
9+
type SnapshotRecordArray = import('@ember-data/store/-private/system/snapshot-record-array').default;
10+
711
/**
812
@module @ember-data/adapter
913
*/
@@ -55,7 +59,13 @@ export default Mixin.create({
5559
@param {Object} query object of query parameters to send for query requests.
5660
@return {String} url
5761
*/
58-
buildURL(modelName, id, snapshot, requestType, query) {
62+
buildURL(
63+
modelName: string,
64+
id: string | string[] | Dict<unknown> | null,
65+
snapshot: Snapshot | Snapshot[] | SnapshotRecordArray | null,
66+
requestType: string = '',
67+
query = {}
68+
): string {
5969
switch (requestType) {
6070
case 'findRecord':
6171
return this.urlForFindRecord(id, modelName, snapshot);
@@ -89,9 +99,9 @@ export default Mixin.create({
8999
@param {String} id
90100
@return {String} url
91101
*/
92-
_buildURL(modelName, id) {
102+
_buildURL(modelName: string | null | undefined, id: string | null | undefined): string {
93103
let path;
94-
let url = [];
104+
let url: string[] = [];
95105
let host = get(this, 'host');
96106
let prefix = this.urlPrefix();
97107

@@ -109,12 +119,12 @@ export default Mixin.create({
109119
url.unshift(prefix);
110120
}
111121

112-
url = url.join('/');
113-
if (!host && url && url.charAt(0) !== '/') {
114-
url = '/' + url;
122+
let urlString = url.join('/');
123+
if (!host && urlString && urlString.charAt(0) !== '/') {
124+
urlString = '/' + urlString;
115125
}
116126

117-
return url;
127+
return urlString;
118128
},
119129

120130
/**
@@ -140,7 +150,7 @@ export default Mixin.create({
140150
@return {String} url
141151
142152
*/
143-
urlForFindRecord(id, modelName, snapshot) {
153+
urlForFindRecord(id: string, modelName: string, snapshot: Snapshot): string {
144154
return this._buildURL(modelName, id);
145155
},
146156

@@ -165,7 +175,7 @@ export default Mixin.create({
165175
@param {SnapshotRecordArray} snapshot
166176
@return {String} url
167177
*/
168-
urlForFindAll(modelName, snapshot) {
178+
urlForFindAll(modelName: string, snapshot: Snapshot): string {
169179
return this._buildURL(modelName);
170180
},
171181

@@ -195,7 +205,7 @@ export default Mixin.create({
195205
@param {String} modelName
196206
@return {String} url
197207
*/
198-
urlForQuery(query, modelName) {
208+
urlForQuery(query: Dict<unknown>, modelName: string): string {
199209
return this._buildURL(modelName);
200210
},
201211

@@ -220,7 +230,7 @@ export default Mixin.create({
220230
@param {String} modelName
221231
@return {String} url
222232
*/
223-
urlForQueryRecord(query, modelName) {
233+
urlForQueryRecord(query: Dict<unknown>, modelName: string): string {
224234
return this._buildURL(modelName);
225235
},
226236

@@ -248,7 +258,7 @@ export default Mixin.create({
248258
@param {Array} snapshots
249259
@return {String} url
250260
*/
251-
urlForFindMany(ids, modelName, snapshots) {
261+
urlForFindMany(ids: string[], modelName: string, snapshots: Snapshot[]) {
252262
return this._buildURL(modelName);
253263
},
254264

@@ -275,7 +285,7 @@ export default Mixin.create({
275285
@param {Snapshot} snapshot
276286
@return {String} url
277287
*/
278-
urlForFindHasMany(id, modelName, snapshot) {
288+
urlForFindHasMany(id: string, modelName: string, snapshot: Snapshot): string {
279289
return this._buildURL(modelName, id);
280290
},
281291

@@ -302,7 +312,7 @@ export default Mixin.create({
302312
@param {Snapshot} snapshot
303313
@return {String} url
304314
*/
305-
urlForFindBelongsTo(id, modelName, snapshot) {
315+
urlForFindBelongsTo(id: string, modelName: string, snapshot: Snapshot): string {
306316
return this._buildURL(modelName, id);
307317
},
308318

@@ -327,7 +337,7 @@ export default Mixin.create({
327337
@param {Snapshot} snapshot
328338
@return {String} url
329339
*/
330-
urlForCreateRecord(modelName, snapshot) {
340+
urlForCreateRecord(modelName: string, snapshot: Snapshot) {
331341
return this._buildURL(modelName);
332342
},
333343

@@ -352,7 +362,7 @@ export default Mixin.create({
352362
@param {Snapshot} snapshot
353363
@return {String} url
354364
*/
355-
urlForUpdateRecord(id, modelName, snapshot) {
365+
urlForUpdateRecord(id: string, modelName: string, snapshot: Snapshot): string {
356366
return this._buildURL(modelName, id);
357367
},
358368

@@ -377,7 +387,7 @@ export default Mixin.create({
377387
@param {Snapshot} snapshot
378388
@return {String} url
379389
*/
380-
urlForDeleteRecord(id, modelName, snapshot) {
390+
urlForDeleteRecord(id: string, modelName: string, snapshot: Snapshot): string {
381391
return this._buildURL(modelName, id);
382392
},
383393

@@ -388,7 +398,7 @@ export default Mixin.create({
388398
@param {String} parentURL
389399
@return {String} urlPrefix
390400
*/
391-
urlPrefix(path, parentURL) {
401+
urlPrefix(path: string | null | undefined, parentURL: string): string {
392402
let host = get(this, 'host');
393403
let namespace = get(this, 'namespace');
394404

@@ -412,7 +422,7 @@ export default Mixin.create({
412422
}
413423

414424
// No path provided
415-
let url = [];
425+
let url: string[] = [];
416426
if (host) {
417427
url.push(host);
418428
}
@@ -450,7 +460,7 @@ export default Mixin.create({
450460
@param {String} modelName
451461
@return {String} path
452462
**/
453-
pathForType(modelName) {
463+
pathForType(modelName: string): string {
454464
let camelized = camelize(modelName);
455465
return pluralize(camelized);
456466
},
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface Request {
2+
protocol: string;
3+
host: string;
4+
}
5+
6+
export interface FastBoot {
7+
isFastBoot: boolean;
8+
request: Request;
9+
}

packages/adapter/addon/-private/utils/determine-body-promise.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DEBUG } from '@glimmer/env';
33

44
import continueOnReject from './continue-on-reject';
55

6+
type RequestData = import('../../rest').RequestData;
67
type Payload = object | string | undefined;
78

89
interface CustomSyntaxError extends SyntaxError {
@@ -62,7 +63,7 @@ function _determineContent(response: Response, requestData: JQueryAjaxSettings,
6263
return ret;
6364
}
6465

65-
export function determineBodyPromise(response: Response, requestData: JQueryAjaxSettings): Promise<Payload> {
66+
export function determineBodyPromise(response: Response, requestData: RequestData): Promise<Payload> {
6667
// response.text() may resolve or reject
6768
// it is a native promise, may not have finally
6869
return continueOnReject(response.text()).then(payload => _determineContent(response, requestData, payload));

0 commit comments

Comments
 (0)