This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2111117
commit b268bbd
Showing
34 changed files
with
748 additions
and
782 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "npm", | ||
"script": "test", | ||
"group": { | ||
"kind": "test", | ||
"isDefault": true | ||
} | ||
}, | ||
{ | ||
"type": "npm", | ||
"script": "test:watch", | ||
"group": "test" | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import RSVP, { Promise } from 'rsvp'; | ||
|
||
/** | ||
* AJAX Promise | ||
* | ||
* Sub-class of RSVP Promise that passes the XHR property on to the | ||
* child promise | ||
* | ||
* @extends RSVP.Promise | ||
* @private | ||
*/ | ||
export default class AJAXPromise<T> extends Promise<T> { | ||
xhr?: JQueryXHR; | ||
|
||
// NOTE: Only necessary due to broken definition of RSVP.Promise | ||
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/26640 | ||
constructor( | ||
executor: ( | ||
resolve: (value?: RSVP.Arg<T>) => void, | ||
reject: (reason?: any) => void | ||
) => void, | ||
label?: string | ||
) { | ||
// @ts-ignore | ||
super(executor, label); | ||
} | ||
|
||
/** | ||
* Overriding `.then` to add XHR to child promise | ||
*/ | ||
then<TResult1 = T, TResult2 = never>( | ||
onFulfilled?: | ||
| ((value: T) => TResult1 | PromiseLike<TResult1>) | ||
| undefined | ||
| null, | ||
onRejected?: | ||
| ((reason: any) => TResult2 | PromiseLike<TResult2>) | ||
| undefined | ||
| null, | ||
label?: string | ||
): AJAXPromise<TResult1 | TResult2> { | ||
const child = super.then(onFulfilled, onRejected, label); | ||
|
||
(child as AJAXPromise<TResult1 | TResult2>).xhr = this.xhr; | ||
|
||
return child; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { AjaxError } from '../errors'; | ||
|
||
export interface Headers { | ||
[key: string]: string | undefined | null; | ||
} | ||
|
||
export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'; | ||
|
||
export interface AJAXOptions extends JQueryAjaxSettings { | ||
host?: string; | ||
namespace?: string; | ||
} | ||
|
||
export interface RequestData { | ||
method: string; | ||
type: string; | ||
url?: string; | ||
} | ||
|
||
export type Matcher = string | RegExp; | ||
|
||
export type Response = any; | ||
|
||
export interface RawResponse<T = Response> { | ||
response: T; | ||
jqXHR: JQueryXHR; | ||
payload: object; | ||
textStatus: string; | ||
} | ||
|
||
export interface RawErrorResponse extends RawResponse { | ||
response: AjaxError; | ||
errorThrown?: string; | ||
} |
13 changes: 7 additions & 6 deletions
13
addon/-private/utils/get-header.js → addon/-private/utils/get-header.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
import { A } from '@ember/array'; | ||
import { isNone } from '@ember/utils'; | ||
import { Headers } from '../types'; | ||
|
||
/** | ||
* Do a case-insensitive lookup of an HTTP header | ||
* | ||
* @function getHeader | ||
* @private | ||
* @param {Object} headers | ||
* @param {string} name | ||
* @return {string} | ||
*/ | ||
export default function getHeader(headers, name) { | ||
export default function getHeader( | ||
headers: Headers | undefined, | ||
name: string | undefined | ||
): string | undefined | null { | ||
if (isNone(headers) || isNone(name)) { | ||
return; // ask for nothing, get nothing. | ||
return undefined; | ||
} | ||
|
||
const matchedKey = A(Object.keys(headers)).find(key => { | ||
return key.toLowerCase() === name.toLowerCase(); | ||
}); | ||
|
||
return headers[matchedKey]; | ||
return matchedKey ? headers[matchedKey] : undefined; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function isString(object: any): object is string { | ||
return typeof object === 'string'; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Headers } from '../types'; | ||
|
||
export const CRLF = '\u000d\u000a'; | ||
|
||
export default function parseResponseHeaders(headersString: string): Headers { | ||
const headers = {}; | ||
|
||
if (!headersString) { | ||
return headers; | ||
} | ||
|
||
return headersString.split(CRLF).reduce((hash: Headers, header) => { | ||
let [field, ...value] = header.split(':'); | ||
|
||
field = field.trim(); | ||
|
||
const valueString = value.join(':').trim(); | ||
|
||
if (valueString) { | ||
hash[field] = valueString; | ||
} | ||
|
||
return hash; | ||
}, headers); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* eslint-env browser, node */ | ||
|
||
const completeUrlRegex = /^(http|https)/; | ||
|
||
interface URLObject { | ||
href?: string; | ||
protocol?: string; | ||
hostname?: string; | ||
port?: string; | ||
pathname?: string; | ||
search?: string; | ||
hash?: string; | ||
} | ||
|
||
/** | ||
* Parse a URL string into an object that defines its structure | ||
* | ||
* The returned object will have the following properties: | ||
* | ||
* href: the full URL | ||
* protocol: the request protocol | ||
* hostname: the target for the request | ||
* port: the port for the request | ||
* pathname: any URL after the host | ||
* search: query parameters | ||
* hash: the URL hash | ||
* | ||
* @function parseURL | ||
* @private | ||
*/ | ||
export function parseURL(str: string): URLObject { | ||
let fullObject: URLObject; | ||
|
||
if (typeof FastBoot === 'undefined') { | ||
const element = document.createElement('a'); | ||
element.href = str; | ||
fullObject = element; | ||
} else { | ||
fullObject = FastBoot.require('url').parse(str); | ||
} | ||
|
||
const desiredProps = { | ||
href: fullObject.href, | ||
protocol: fullObject.protocol, | ||
hostname: fullObject.hostname, | ||
port: fullObject.port, | ||
pathname: fullObject.pathname, | ||
search: fullObject.search, | ||
hash: fullObject.hash | ||
}; | ||
|
||
return desiredProps; | ||
} | ||
|
||
export function isFullURL(url: string): boolean { | ||
return !!url.match(completeUrlRegex); | ||
} | ||
|
||
export function haveSameHost(a: string, b: string): boolean { | ||
const urlA = parseURL(a); | ||
const urlB = parseURL(b); | ||
|
||
return ( | ||
urlA.protocol === urlB.protocol && | ||
urlA.hostname === urlB.hostname && | ||
urlA.port === urlB.port | ||
); | ||
} |
File renamed without changes.
Oops, something went wrong.