Skip to content

Commit 4f7afbd

Browse files
author
Patrick Sachs
authored
Merge pull request #5 from MatthiasDummler/master
Added: Default URL init
2 parents 572ff47 + 6633212 commit 4f7afbd

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

dist/index.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ declare type AjaxRequestInit = RequestInit & {
1111
*/
1212
export declare const defaultInit: Partial<AjaxRequestInit> & {
1313
headers: Record<string, string>;
14+
url?: Partial<AjaxURLObject>;
1415
};
1516
declare type URLComponent = string | number | boolean | null | undefined;
16-
/**
17-
* A valid URL for an ajax request. Can either directly be a string representing a raw URL or a object to safely build it (recommended).
18-
*/
19-
export declare type AjaxURL = string | {
17+
declare type AjaxURLObject = {
2018
/**
2119
* The base URL of the request.
2220
* Example: `https://www.sahnee.games/request?param1=true&params2=hello%20world&param2=second#test`
@@ -70,6 +68,10 @@ export declare type AjaxURL = string | {
7068
[key: string]: URLComponent | URLComponent[];
7169
};
7270
};
71+
/**
72+
* A valid URL for an ajax request. Can either directly be a string representing a raw URL or a object to safely build it (recommended).
73+
*/
74+
export declare type AjaxURL = string | AjaxURLObject;
7375
/**
7476
* Helper for inline building a URL.
7577
* @param url The URL.

dist/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ exports.url = function (p) {
9595
if (typeof p === 'string') {
9696
return p;
9797
}
98+
p = Object.assign({}, exports.defaultInit.url, p);
9899
var url = '';
99100
if (Array.isArray(p.url)) {
100101
url = p.url

src/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@ const JSON_CONTENT_TYPE_UTF8 = JSON_CONTENT_TYPE_BASIC + '; charset=utf-8';
2020
/**
2121
* Default options for every request.
2222
*/
23-
export const defaultInit: Partial<AjaxRequestInit> & { headers: Record<string, string> } = {
23+
export const defaultInit: Partial<AjaxRequestInit> & { headers: Record<string, string>, url?: Partial<AjaxURLObject> } = {
2424
headers: {}
2525
};
2626

2727
type URLComponent = string | number | boolean | null | undefined;
2828

29-
/**
30-
* A valid URL for an ajax request. Can either directly be a string representing a raw URL or a object to safely build it (recommended).
31-
*/
32-
export type AjaxURL = string | {
29+
type AjaxURLObject = {
3330
/**
3431
* The base URL of the request.
3532
* Example: `https://www.sahnee.games/request?param1=true&params2=hello%20world&param2=second#test`
@@ -91,6 +88,11 @@ export type AjaxURL = string | {
9188
}
9289
};
9390

91+
/**
92+
* A valid URL for an ajax request. Can either directly be a string representing a raw URL or a object to safely build it (recommended).
93+
*/
94+
export type AjaxURL = string | AjaxURLObject
95+
9496
/**
9597
* Helper for inline building a URL.
9698
* @param url The URL.
@@ -100,6 +102,7 @@ export const url = (p: AjaxURL) => {
100102
if (typeof p === 'string') {
101103
return p;
102104
}
105+
p = Object.assign({}, defaultInit.url, p);
103106
let url = '';
104107
if (Array.isArray(p.url)) {
105108
url = p.url

0 commit comments

Comments
 (0)