Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
refactor: convert to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Jun 28, 2018
1 parent 23705b9 commit f71854f
Show file tree
Hide file tree
Showing 34 changed files with 748 additions and 782 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -48,6 +48,8 @@ jobs:
script: yarn lint:js
- stage: lint
script: yarn commitlint-travis
- stage: lint
script: yarn ember ts:precompile

script:
# Usually, it's ok to finish the test scenario without reverting
Expand Down
20 changes: 20 additions & 0 deletions .vscode/tasks.json
@@ -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"
}
]
}
26 changes: 0 additions & 26 deletions addon/-private/promise.js

This file was deleted.

48 changes: 48 additions & 0 deletions addon/-private/promise.ts
@@ -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;
}
}
34 changes: 34 additions & 0 deletions addon/-private/types.ts
@@ -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;
}
@@ -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;
}
3 changes: 0 additions & 3 deletions addon/-private/utils/is-fastboot.js

This file was deleted.

3 changes: 0 additions & 3 deletions addon/-private/utils/is-string.js

This file was deleted.

3 changes: 3 additions & 0 deletions addon/-private/utils/is-string.ts
@@ -0,0 +1,3 @@
export default function isString(object: any): object is string {
return typeof object === 'string';
}
22 changes: 0 additions & 22 deletions addon/-private/utils/parse-response-headers.js

This file was deleted.

25 changes: 25 additions & 0 deletions addon/-private/utils/parse-response-headers.ts
@@ -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);
}
81 changes: 0 additions & 81 deletions addon/-private/utils/url-helpers.js

This file was deleted.

68 changes: 68 additions & 0 deletions addon/-private/utils/url-helpers.ts
@@ -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.

0 comments on commit f71854f

Please sign in to comment.