Skip to content

Commit

Permalink
[Chore]: TypeScript adapters (#7358)
Browse files Browse the repository at this point in the history
* [Chore]: ts adapters


Co-authored-by: Igor Terzic <[email protected]>
Co-authored-by: Scott Newcomer <[email protected]>
  • Loading branch information
3 people authored Feb 6, 2021
1 parent 5c661db commit 22a8f20
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module('Integration | Identifiers - scenarios', function(hooks) {
module('Secondary Cache based on an attribute', function(hooks) {
let store;
let calls;
let isQuery = false;
let secondaryCache: {
id: ConfidentDict<string>;
username: ConfidentDict<string>;
Expand All @@ -44,10 +45,11 @@ module('Integration | Identifiers - scenarios', function(hooks) {
shouldBackgroundReloadRecord() {
return false;
}
findRecord(isQuery = false) {
findRecord() {
if (isQuery !== true) {
calls.findRecord++;
}
isQuery = false;
return resolve({
data: {
id: '1',
Expand All @@ -62,7 +64,8 @@ module('Integration | Identifiers - scenarios', function(hooks) {
}
queryRecord() {
calls.queryRecord++;
return this.findRecord(true);
isQuery = true;
return this.findRecord();
}
}

Expand Down Expand Up @@ -234,6 +237,7 @@ module('Integration | Identifiers - scenarios', function(hooks) {
module('Secondary Cache using an attribute as an alternate id', function(hooks) {
let store;
let calls;
let isQuery = false;
let secondaryCache: ConfidentDict<string>;
class TestSerializer extends Serializer {
normalizeResponse(_, __, payload) {
Expand All @@ -244,10 +248,11 @@ module('Integration | Identifiers - scenarios', function(hooks) {
shouldBackgroundReloadRecord() {
return false;
}
findRecord(isQuery = false) {
findRecord() {
if (isQuery !== true) {
calls.findRecord++;
}
isQuery = false;
return resolve({
data: {
id: '1',
Expand All @@ -262,7 +267,8 @@ module('Integration | Identifiers - scenarios', function(hooks) {
}
queryRecord() {
calls.queryRecord++;
return this.findRecord(true);
isQuery = true;
return this.findRecord();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { camelize } from '@ember/string';

import { pluralize } from 'ember-inflector';

type Dict<T> = import('@ember-data/store/-private/ts-interfaces/utils').Dict<T>;
type Snapshot = import('@ember-data/store/-private/system/snapshot').default;
type SnapshotRecordArray = import('@ember-data/store/-private/system/snapshot-record-array').default;

/**
@module @ember-data/adapter
*/
Expand Down Expand Up @@ -55,7 +59,13 @@ export default Mixin.create({
@param {Object} query object of query parameters to send for query requests.
@return {String} url
*/
buildURL(modelName, id, snapshot, requestType, query) {
buildURL(
modelName: string,
id: string | string[] | Dict<unknown> | null,
snapshot: Snapshot | Snapshot[] | SnapshotRecordArray | null,
requestType: string = '',
query = {}
): string {
switch (requestType) {
case 'findRecord':
return this.urlForFindRecord(id, modelName, snapshot);
Expand Down Expand Up @@ -89,9 +99,9 @@ export default Mixin.create({
@param {String} id
@return {String} url
*/
_buildURL(modelName, id) {
_buildURL(modelName: string | null | undefined, id: string | null | undefined): string {
let path;
let url = [];
let url: string[] = [];
let host = get(this, 'host');
let prefix = this.urlPrefix();

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

url = url.join('/');
if (!host && url && url.charAt(0) !== '/') {
url = '/' + url;
let urlString = url.join('/');
if (!host && urlString && urlString.charAt(0) !== '/') {
urlString = '/' + urlString;
}

return url;
return urlString;
},

/**
Expand All @@ -140,7 +150,7 @@ export default Mixin.create({
@return {String} url
*/
urlForFindRecord(id, modelName, snapshot) {
urlForFindRecord(id: string, modelName: string, snapshot: Snapshot): string {
return this._buildURL(modelName, id);
},

Expand All @@ -165,7 +175,7 @@ export default Mixin.create({
@param {SnapshotRecordArray} snapshot
@return {String} url
*/
urlForFindAll(modelName, snapshot) {
urlForFindAll(modelName: string, snapshot: Snapshot): string {
return this._buildURL(modelName);
},

Expand Down Expand Up @@ -195,7 +205,7 @@ export default Mixin.create({
@param {String} modelName
@return {String} url
*/
urlForQuery(query, modelName) {
urlForQuery(query: Dict<unknown>, modelName: string): string {
return this._buildURL(modelName);
},

Expand All @@ -220,7 +230,7 @@ export default Mixin.create({
@param {String} modelName
@return {String} url
*/
urlForQueryRecord(query, modelName) {
urlForQueryRecord(query: Dict<unknown>, modelName: string): string {
return this._buildURL(modelName);
},

Expand Down Expand Up @@ -248,7 +258,7 @@ export default Mixin.create({
@param {Array} snapshots
@return {String} url
*/
urlForFindMany(ids, modelName, snapshots) {
urlForFindMany(ids: string[], modelName: string, snapshots: Snapshot[]) {
return this._buildURL(modelName);
},

Expand All @@ -275,7 +285,7 @@ export default Mixin.create({
@param {Snapshot} snapshot
@return {String} url
*/
urlForFindHasMany(id, modelName, snapshot) {
urlForFindHasMany(id: string, modelName: string, snapshot: Snapshot): string {
return this._buildURL(modelName, id);
},

Expand All @@ -302,7 +312,7 @@ export default Mixin.create({
@param {Snapshot} snapshot
@return {String} url
*/
urlForFindBelongsTo(id, modelName, snapshot) {
urlForFindBelongsTo(id: string, modelName: string, snapshot: Snapshot): string {
return this._buildURL(modelName, id);
},

Expand All @@ -327,7 +337,7 @@ export default Mixin.create({
@param {Snapshot} snapshot
@return {String} url
*/
urlForCreateRecord(modelName, snapshot) {
urlForCreateRecord(modelName: string, snapshot: Snapshot) {
return this._buildURL(modelName);
},

Expand All @@ -352,7 +362,7 @@ export default Mixin.create({
@param {Snapshot} snapshot
@return {String} url
*/
urlForUpdateRecord(id, modelName, snapshot) {
urlForUpdateRecord(id: string, modelName: string, snapshot: Snapshot): string {
return this._buildURL(modelName, id);
},

Expand All @@ -377,7 +387,7 @@ export default Mixin.create({
@param {Snapshot} snapshot
@return {String} url
*/
urlForDeleteRecord(id, modelName, snapshot) {
urlForDeleteRecord(id: string, modelName: string, snapshot: Snapshot): string {
return this._buildURL(modelName, id);
},

Expand All @@ -388,7 +398,7 @@ export default Mixin.create({
@param {String} parentURL
@return {String} urlPrefix
*/
urlPrefix(path, parentURL) {
urlPrefix(path: string | null | undefined, parentURL: string): string {
let host = get(this, 'host');
let namespace = get(this, 'namespace');

Expand All @@ -412,7 +422,7 @@ export default Mixin.create({
}

// No path provided
let url = [];
let url: string[] = [];
if (host) {
url.push(host);
}
Expand Down Expand Up @@ -450,7 +460,7 @@ export default Mixin.create({
@param {String} modelName
@return {String} path
**/
pathForType(modelName) {
pathForType(modelName: string): string {
let camelized = camelize(modelName);
return pluralize(camelized);
},
Expand Down
9 changes: 9 additions & 0 deletions packages/adapter/addon/-private/fastboot-interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Request {
protocol: string;
host: string;
}

export interface FastBoot {
isFastBoot: boolean;
request: Request;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DEBUG } from '@glimmer/env';

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

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

interface CustomSyntaxError extends SyntaxError {
Expand Down Expand Up @@ -62,7 +63,7 @@ function _determineContent(response: Response, requestData: JQueryAjaxSettings,
return ret;
}

export function determineBodyPromise(response: Response, requestData: JQueryAjaxSettings): Promise<Payload> {
export function determineBodyPromise(response: Response, requestData: RequestData): Promise<Payload> {
// response.text() may resolve or reject
// it is a native promise, may not have finally
return continueOnReject(response.text()).then(payload => _determineContent(response, requestData, payload));
Expand Down
Loading

0 comments on commit 22a8f20

Please sign in to comment.