Skip to content

Commit

Permalink
Update naming for extended types
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Jan 29, 2021
1 parent 1de09f6 commit 6aa47f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
6 changes: 3 additions & 3 deletions packages/adapter/addon/json-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { pluralize } from 'ember-inflector';
import { serializeIntoHash } from './-private';
import RESTAdapter from './rest';

type FetchRequestInit = import('./rest').FetchRequestInit;
type JQueryRequestInit = import('./rest').JQueryRequestInit;
type CustomFetchOptions = import('./rest').CustomFetchOptions;
type CustomJQueryAjaxSettings = import('./rest').CustomJQueryAjaxSettings;

type Dict<T> = import('@ember-data/store/-private/ts-interfaces/utils').Dict<T>;
type ShimModelClass = import('@ember-data/store/-private/system/model/shim-model-class').default;
Expand Down Expand Up @@ -168,7 +168,7 @@ class JSONAPIAdapter extends RESTAdapter {
url: string,
type: string,
options: JQueryAjaxSettings | RequestInit = {}
): JQueryRequestInit | FetchRequestInit {
): CustomJQueryAjaxSettings | CustomFetchOptions {
let hash = super.ajaxOptions(url, type, options);

hash.headers['Accept'] = hash.headers['Accept'] || 'application/vnd.api+json';
Expand Down
34 changes: 20 additions & 14 deletions packages/adapter/addon/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ type QueryState = {
since?: unknown;
};

export interface FetchRequestInit extends RequestInit {
// We need to extend the original types in order to work with our system.
// For example, whether a user uses fetch or jquery, their options object may contain `data.
// data is not something fetch can work with but we need it to serialize to the url.
export interface CustomFetchOptions extends RequestInit {
url: string;
method: string;
type: string;
Expand All @@ -52,7 +55,7 @@ export interface FetchRequestInit extends RequestInit {
headers?: any;
}

export interface JQueryRequestInit extends JQueryAjaxSettings {
export interface CustomJQueryAjaxSettings extends JQueryAjaxSettings {
url: string;
method: string;
type: string;
Expand Down Expand Up @@ -1049,7 +1052,7 @@ class RESTAdapter extends Adapter.extend(BuildURLMixin) {

if (this.useFetch) {
let _response;
let hash: FetchRequestInit = adapter.ajaxOptions(url, type, options);
let hash: CustomFetchOptions = adapter.ajaxOptions(url, type, options);
return this._fetchRequest(hash)
.then((response: Response) => {
_response = response;
Expand All @@ -1063,7 +1066,7 @@ class RESTAdapter extends Adapter.extend(BuildURLMixin) {
}
});
} else {
let hash: JQueryRequestInit = adapter.ajaxOptions(url, type, options);
let hash: CustomJQueryAjaxSettings = adapter.ajaxOptions(url, type, options);

return new RSVPPromise(function(resolve, reject) {
hash.success = function(payload, textStatus, jqXHR) {
Expand All @@ -1087,11 +1090,14 @@ class RESTAdapter extends Adapter.extend(BuildURLMixin) {
@param {Object} options jQuery ajax options to be used for the ajax request
*/
_ajaxRequest(options: JQueryAjaxSettings): void {
// TODO add assertion that jquery is there rather then equality check
typeof jQuery !== 'undefined' && jQuery.ajax(options);
if (jQuery) {
jQuery.ajax(options);
} else {
throw new Error('cannot find the `jQuery` global. Did you mean to install the `ember-ajax` addon?');
}
}

_fetchRequest(options: FetchRequestInit): Promise<Response> {
_fetchRequest(options: CustomFetchOptions): Promise<Response> {
let fetchFunction = fetch();

if (fetchFunction) {
Expand All @@ -1103,9 +1109,9 @@ class RESTAdapter extends Adapter.extend(BuildURLMixin) {
}
}

_ajax(options: FetchRequestInit | JQueryAjaxSettings): void {
_ajax(options: CustomFetchOptions | JQueryAjaxSettings): void {
if (this.useFetch) {
this._fetchRequest(options as FetchRequestInit);
this._fetchRequest(options as CustomFetchOptions);
} else if (DEPRECATE_NAJAX && this.fastboot && this.fastboot.isFastBoot) {
this._najaxRequest(options);
} else {
Expand All @@ -1125,8 +1131,8 @@ class RESTAdapter extends Adapter.extend(BuildURLMixin) {
url: string,
method: string,
options: JQueryAjaxSettings | RequestInit
): JQueryRequestInit | FetchRequestInit {
let reqOptions: JQueryRequestInit | FetchRequestInit = assign(
): CustomJQueryAjaxSettings | CustomFetchOptions {
let reqOptions: CustomJQueryAjaxSettings | CustomFetchOptions = assign(
{
url,
method,
Expand Down Expand Up @@ -1440,9 +1446,9 @@ function headersToObject(headers: Headers): Dict<unknown> {
* @returns {Object}
*/
export function fetchOptions(
options: JQueryRequestInit & Partial<FetchRequestInit>,
options: CustomJQueryAjaxSettings & Partial<CustomFetchOptions>,
adapter: RESTAdapter
): FetchRequestInit {
): CustomFetchOptions {
options.credentials = options.credentials || 'same-origin';

if (options.data) {
Expand Down Expand Up @@ -1476,7 +1482,7 @@ export function fetchOptions(
return options;
}

function ajaxOptions(options: JQueryRequestInit, adapter: RESTAdapter): JQueryRequestInit {
function ajaxOptions(options: CustomJQueryAjaxSettings, adapter: RESTAdapter): CustomJQueryAjaxSettings {
options.dataType = 'json';
options.context = adapter;

Expand Down

0 comments on commit 6aa47f8

Please sign in to comment.