ozone-api-request
Advanced tools
Comparing version 5.4.20 to 5.4.21
@@ -15,3 +15,2 @@ export declare type OzoneAPIRequestOption = { | ||
* | ||
* * *ozone-api-request-timeout* Fired when connection timeout. | ||
@@ -81,11 +80,21 @@ * Event detail contains the XMLHttpRequest. | ||
/** | ||
* setup OzoneAPIRequest global option. | ||
* @param option: OzoneAPIRequestOption default value { cache: false } | ||
* eventTarget to dispatch *ozone-api-request-success* and *ozone-api-request-error* events | ||
* Default value is document. | ||
* @type {EventTarget} | ||
*/ | ||
static setup(option: OzoneAPIRequestOption): void; | ||
private _url; | ||
eventTarget: EventTarget | null; | ||
private _resultPromise?; | ||
private currentRequest; | ||
private resolveCurrentRequest?; | ||
constructor(); | ||
url: string; | ||
private _method; | ||
get method(): string; | ||
set method(method: string); | ||
private _body; | ||
get body(): string | FormData; | ||
set body(body: string | FormData); | ||
private _responseType; | ||
private _resultPromise?; | ||
get responseType(): XMLHttpRequestResponseType; | ||
set responseType(responseType: XMLHttpRequestResponseType); | ||
/** | ||
@@ -95,18 +104,12 @@ * Resolve with current XMLHttpRequest on achieved | ||
get result(): Promise<XMLHttpRequest>; | ||
set url(url: string); | ||
get url(): string; | ||
set body(body: string | FormData); | ||
get body(): string | FormData; | ||
set method(method: string); | ||
get method(): string; | ||
set responseType(responseType: XMLHttpRequestResponseType); | ||
get responseType(): XMLHttpRequestResponseType; | ||
private currentRequest; | ||
private _onreadystatechange; | ||
set onreadystatechange(callback: ((this: XMLHttpRequest, ev: Event) => any)); | ||
private resolveCurrentRequest?; | ||
abort(): void; | ||
get readyState(): number; | ||
constructor(); | ||
/** | ||
* setup OzoneAPIRequest global option. | ||
* @param option: OzoneAPIRequestOption default value { cache: false } | ||
*/ | ||
static setup(option: OzoneAPIRequestOption): void; | ||
abort(): void; | ||
/** | ||
* Create and open an XMLHttpRequest | ||
@@ -116,10 +119,3 @@ * @return {XMLHttpRequest} | ||
createXMLHttpRequest(withHeader?: boolean): XMLHttpRequest; | ||
/** | ||
* eventTarget to dispatch *ozone-api-request-success* and *ozone-api-request-error* events | ||
* Default value is document. | ||
* @type {EventTarget} | ||
*/ | ||
eventTarget: EventTarget | null; | ||
setEventTarget(element: EventTarget): void; | ||
private dispatchEvent; | ||
/** | ||
@@ -137,2 +133,3 @@ * | ||
sendRequest(request?: XMLHttpRequest): Promise<XMLHttpRequest>; | ||
private dispatchEvent; | ||
} |
@@ -12,3 +12,2 @@ /** | ||
* | ||
* * *ozone-api-request-timeout* Fired when connection timeout. | ||
@@ -77,7 +76,7 @@ * Event detail contains the XMLHttpRequest. | ||
constructor() { | ||
this._url = ''; | ||
this.currentRequest = null; | ||
this.url = ''; | ||
this._method = 'GET'; | ||
this._body = ''; | ||
this._responseType = 'json'; | ||
this.currentRequest = null; | ||
this._onreadystatechange = null; | ||
@@ -92,59 +91,58 @@ try { | ||
} | ||
/** | ||
* setup OzoneAPIRequest global option. | ||
* @param option: OzoneAPIRequestOption default value { cache: false } | ||
*/ | ||
static setup(option) { | ||
OzoneAPIRequest.option = option; | ||
get method() { | ||
return this._method; | ||
} | ||
/** | ||
* Resolve with current XMLHttpRequest on achieved | ||
*/ | ||
get result() { | ||
if (this._resultPromise) | ||
return this._resultPromise; | ||
else | ||
throw new Error("Request has not been send"); | ||
set method(method) { | ||
this._method = method; | ||
} | ||
set url(url) { | ||
this._url = url; | ||
get body() { | ||
return this._body; | ||
} | ||
get url() { | ||
let url = this._url; | ||
if (!OzoneAPIRequest.option.cache && this.method === 'GET') { | ||
const [urlPath, param] = url.split('?'); | ||
const urlParam = new URLSearchParams(param); | ||
urlParam.append('_', Date.now().toString()); | ||
url = [urlPath, urlParam.toString()].join('?'); | ||
} | ||
return url; | ||
} | ||
set body(body) { | ||
this._body = body; | ||
} | ||
get body() { return this._body; } | ||
set method(method) { | ||
this._method = method; | ||
get responseType() { | ||
return this._responseType; | ||
} | ||
get method() { return this._method; } | ||
set responseType(responseType) { | ||
this._responseType = responseType; | ||
} | ||
get responseType() { return this._responseType; } | ||
/** | ||
* Resolve with current XMLHttpRequest on achieved | ||
*/ | ||
get result() { | ||
if (this._resultPromise) { | ||
return this._resultPromise; | ||
} | ||
else { | ||
throw new Error('Request has not been send'); | ||
} | ||
} | ||
set onreadystatechange(callback) { | ||
this._onreadystatechange = callback; | ||
} | ||
abort() { | ||
if (this.currentRequest) | ||
this.currentRequest.abort(); | ||
if (this.resolveCurrentRequest) | ||
this.resolveCurrentRequest(this.currentRequest); | ||
} | ||
get readyState() { | ||
if (this.currentRequest) | ||
if (this.currentRequest) { | ||
return this.currentRequest.readyState; | ||
else | ||
} | ||
else { | ||
return 0; | ||
} | ||
} | ||
/** | ||
* setup OzoneAPIRequest global option. | ||
* @param option: OzoneAPIRequestOption default value { cache: false } | ||
*/ | ||
static setup(option) { | ||
OzoneAPIRequest.option = option; | ||
} | ||
abort() { | ||
if (this.currentRequest) { | ||
this.currentRequest.abort(); | ||
} | ||
if (this.resolveCurrentRequest) { | ||
this.resolveCurrentRequest(this.currentRequest); | ||
} | ||
} | ||
/** | ||
* Create and open an XMLHttpRequest | ||
@@ -159,3 +157,3 @@ * @return {XMLHttpRequest} | ||
if (withHeader) { | ||
xmlhttp.setRequestHeader("Content-Type", "application/json"); | ||
xmlhttp.setRequestHeader('Content-Type', 'application/json'); | ||
xmlhttp.setRequestHeader('Accept', 'application/json'); | ||
@@ -168,9 +166,2 @@ } | ||
} | ||
dispatchEvent(eventName, xmlhttp) { | ||
if (this.eventTarget) { | ||
this.eventTarget.dispatchEvent(new CustomEvent(eventName, { | ||
bubbles: true, detail: xmlhttp | ||
})); | ||
} | ||
} | ||
/** | ||
@@ -183,4 +174,5 @@ * | ||
const xmlhttp = this.currentRequest = request || this.createXMLHttpRequest(); | ||
if (this._onreadystatechange) | ||
if (this._onreadystatechange) { | ||
xmlhttp.onreadystatechange = this._onreadystatechange; | ||
} | ||
this._resultPromise = new Promise((resolve, reject) => { | ||
@@ -220,2 +212,9 @@ this.resolveCurrentRequest = resolve; | ||
} | ||
dispatchEvent(eventName, xmlhttp) { | ||
if (this.eventTarget) { | ||
this.eventTarget.dispatchEvent(new CustomEvent(eventName, { | ||
bubbles: true, detail: xmlhttp | ||
})); | ||
} | ||
} | ||
} | ||
@@ -222,0 +221,0 @@ OzoneAPIRequest.option = { |
{ | ||
"name": "ozone-api-request", | ||
"version": "5.4.20", | ||
"version": "5.4.21", | ||
"publishConfig": { | ||
@@ -25,3 +25,3 @@ "access": "public" | ||
}, | ||
"gitHead": "f4a2ff0fb531e0ffdff104da770338989689c77c" | ||
"gitHead": "7a52ece18320f418263d070eac1475ccefe4fbeb" | ||
} |
export type OzoneAPIRequestOption = { | ||
cache: boolean | ||
cache: boolean | ||
} | ||
@@ -16,3 +16,2 @@ | ||
* | ||
* * *ozone-api-request-timeout* Fired when connection timeout. | ||
@@ -79,180 +78,180 @@ * Event detail contains the XMLHttpRequest. | ||
*/ | ||
export class OzoneAPIRequest{ | ||
export class OzoneAPIRequest { | ||
private static option: OzoneAPIRequestOption = { | ||
cache: true | ||
}; | ||
private static option: OzoneAPIRequestOption = { | ||
cache: true | ||
} | ||
/** | ||
* eventTarget to dispatch *ozone-api-request-success* and *ozone-api-request-error* events | ||
* Default value is document. | ||
* @type {EventTarget} | ||
*/ | ||
eventTarget: EventTarget | null | ||
private _resultPromise?: Promise<XMLHttpRequest> | ||
private currentRequest: XMLHttpRequest | null = null | ||
private resolveCurrentRequest?: { (...param: Array<any>): void } | ||
/** | ||
* setup OzoneAPIRequest global option. | ||
* @param option: OzoneAPIRequestOption default value { cache: false } | ||
*/ | ||
static setup(option: OzoneAPIRequestOption){ | ||
OzoneAPIRequest.option = option | ||
} | ||
constructor() { | ||
try { | ||
// should not work for nodejs | ||
this.eventTarget = document | ||
} catch (err) { | ||
this.eventTarget = null | ||
} | ||
} | ||
private _url: string =''; | ||
private _method: string = 'GET'; | ||
private _body: string | FormData = ''; | ||
private _responseType: XMLHttpRequestResponseType = 'json'; | ||
public url: string = '' | ||
private _resultPromise?: Promise<XMLHttpRequest>; | ||
private _method: string = 'GET' | ||
/** | ||
* Resolve with current XMLHttpRequest on achieved | ||
*/ | ||
get result(): Promise<XMLHttpRequest>{ | ||
if(this._resultPromise) | ||
return this._resultPromise; | ||
else | ||
throw new Error("Request has not been send") | ||
} | ||
get method(): string { | ||
return this._method | ||
} | ||
set method(method: string) { | ||
this._method = method | ||
} | ||
set url (url: string){ | ||
this._url = url; | ||
} | ||
get url(): string{ | ||
let url = this._url; | ||
if (! OzoneAPIRequest.option.cache && this.method === 'GET'){ | ||
const [urlPath, param] = url.split('?'); | ||
const urlParam = new URLSearchParams(param); | ||
urlParam.append('_', Date.now().toString()); | ||
url = [urlPath, urlParam.toString()].join('?'); | ||
} | ||
return url | ||
} | ||
private _body: string | FormData = '' | ||
set body (body: string | FormData){ | ||
this._body = body; | ||
} | ||
get body (): string | FormData { return this._body} | ||
get body(): string | FormData { | ||
return this._body | ||
} | ||
set method(method:string){ | ||
this._method = method; | ||
} | ||
get method (): string {return this._method;} | ||
set body(body: string | FormData) { | ||
this._body = body | ||
} | ||
set responseType(responseType:XMLHttpRequestResponseType){ | ||
this._responseType = responseType; | ||
} | ||
get responseType (): XMLHttpRequestResponseType {return this._responseType;} | ||
private _responseType: XMLHttpRequestResponseType = 'json' | ||
private currentRequest: XMLHttpRequest | null = null ; | ||
get responseType(): XMLHttpRequestResponseType { | ||
return this._responseType | ||
} | ||
private _onreadystatechange: ((this: XMLHttpRequest, ev: Event) => any) | null = null; | ||
set responseType(responseType: XMLHttpRequestResponseType) { | ||
this._responseType = responseType | ||
} | ||
set onreadystatechange(callback: ((this: XMLHttpRequest, ev: Event) => any)){ | ||
this._onreadystatechange = callback; | ||
} | ||
/** | ||
* Resolve with current XMLHttpRequest on achieved | ||
*/ | ||
get result(): Promise<XMLHttpRequest> { | ||
if (this._resultPromise) { | ||
return this._resultPromise | ||
} else { | ||
throw new Error('Request has not been send') | ||
} | ||
} | ||
private resolveCurrentRequest?: {(...param: Array<any>):void}; | ||
abort(){ | ||
if(this.currentRequest) | ||
this.currentRequest.abort(); | ||
if(this.resolveCurrentRequest) | ||
this.resolveCurrentRequest(this.currentRequest) | ||
} | ||
private _onreadystatechange: ((this: XMLHttpRequest, ev: Event) => any) | null = null | ||
get readyState (): number{ | ||
if(this.currentRequest) | ||
return this.currentRequest.readyState ; | ||
else | ||
return 0 | ||
} | ||
constructor(){ | ||
try { | ||
// should not work for nodejs | ||
this.eventTarget = document | ||
} | ||
catch(err) { | ||
this.eventTarget = null | ||
} | ||
} | ||
/** | ||
* Create and open an XMLHttpRequest | ||
* @return {XMLHttpRequest} | ||
*/ | ||
createXMLHttpRequest(withHeader: boolean = true): XMLHttpRequest{ | ||
const xmlhttp = new XMLHttpRequest(); | ||
xmlhttp.withCredentials = true; | ||
set onreadystatechange(callback: ((this: XMLHttpRequest, ev: Event) => any)) { | ||
this._onreadystatechange = callback | ||
} | ||
xmlhttp.open(this.method, this.url, true); | ||
xmlhttp.responseType = this.responseType; | ||
if(withHeader) { | ||
xmlhttp.setRequestHeader("Content-Type", "application/json"); | ||
xmlhttp.setRequestHeader('Accept', 'application/json'); | ||
} | ||
return xmlhttp; | ||
} | ||
get readyState(): number { | ||
if (this.currentRequest) { | ||
return this.currentRequest.readyState | ||
} else { | ||
return 0 | ||
} | ||
} | ||
/** | ||
* eventTarget to dispatch *ozone-api-request-success* and *ozone-api-request-error* events | ||
* Default value is document. | ||
* @type {EventTarget} | ||
*/ | ||
eventTarget: EventTarget | null; | ||
setEventTarget(element: EventTarget){ | ||
this.eventTarget = element; | ||
} | ||
/** | ||
* setup OzoneAPIRequest global option. | ||
* @param option: OzoneAPIRequestOption default value { cache: false } | ||
*/ | ||
static setup(option: OzoneAPIRequestOption) { | ||
OzoneAPIRequest.option = option | ||
} | ||
private dispatchEvent(eventName: string, xmlhttp: XMLHttpRequest){ | ||
if(this.eventTarget){ | ||
this.eventTarget.dispatchEvent(new CustomEvent<XMLHttpRequest>(eventName, { | ||
bubbles: true, detail: xmlhttp | ||
})); | ||
} | ||
} | ||
abort() { | ||
if (this.currentRequest) { | ||
this.currentRequest.abort() | ||
} | ||
if (this.resolveCurrentRequest) { | ||
this.resolveCurrentRequest(this.currentRequest) | ||
} | ||
} | ||
/** | ||
* | ||
* @param {XMLHttpRequest} request (optional) This parameters overwrite the default XmlHttpRequest. | ||
* @return {OzoneAPIRequest} | ||
*/ | ||
send(request?: XMLHttpRequest):OzoneAPIRequest{ | ||
const xmlhttp = this.currentRequest = request || this.createXMLHttpRequest(); | ||
/** | ||
* Create and open an XMLHttpRequest | ||
* @return {XMLHttpRequest} | ||
*/ | ||
createXMLHttpRequest(withHeader: boolean = true): XMLHttpRequest { | ||
const xmlhttp = new XMLHttpRequest() | ||
xmlhttp.withCredentials = true | ||
if(this._onreadystatechange) | ||
xmlhttp.onreadystatechange = this._onreadystatechange; | ||
xmlhttp.open(this.method, this.url, true) | ||
xmlhttp.responseType = this.responseType | ||
if (withHeader) { | ||
xmlhttp.setRequestHeader('Content-Type', 'application/json') | ||
xmlhttp.setRequestHeader('Accept', 'application/json') | ||
} | ||
return xmlhttp | ||
} | ||
setEventTarget(element: EventTarget) { | ||
this.eventTarget = element | ||
} | ||
this._resultPromise = new Promise((resolve, reject)=>{ | ||
this.resolveCurrentRequest = resolve; | ||
/** | ||
* | ||
* @param {XMLHttpRequest} request (optional) This parameters overwrite the default XmlHttpRequest. | ||
* @return {OzoneAPIRequest} | ||
*/ | ||
send(request?: XMLHttpRequest): OzoneAPIRequest { | ||
const xmlhttp = this.currentRequest = request || this.createXMLHttpRequest() | ||
const handleResponse = ()=>{ | ||
if (xmlhttp.status >= 200 && xmlhttp.status < 300) { | ||
this.dispatchEvent('ozone-api-request-success', xmlhttp); | ||
resolve(xmlhttp); | ||
} else if(xmlhttp.status === 403) { | ||
this.dispatchEvent('ozone-api-request-unauthorized', xmlhttp); | ||
reject(xmlhttp); | ||
} else { | ||
this.dispatchEvent('ozone-api-request-error', xmlhttp); | ||
reject(xmlhttp); | ||
} | ||
}; | ||
if (this._onreadystatechange) { | ||
xmlhttp.onreadystatechange = this._onreadystatechange | ||
} | ||
xmlhttp.onload = handleResponse; | ||
this._resultPromise = new Promise((resolve, reject) => { | ||
this.resolveCurrentRequest = resolve | ||
xmlhttp.ontimeout = () =>{ | ||
this.dispatchEvent('ozone-api-request-timeout', xmlhttp); | ||
reject(xmlhttp); | ||
}; | ||
const handleResponse = () => { | ||
if (xmlhttp.status >= 200 && xmlhttp.status < 300) { | ||
this.dispatchEvent('ozone-api-request-success', xmlhttp) | ||
resolve(xmlhttp) | ||
} else if (xmlhttp.status === 403) { | ||
this.dispatchEvent('ozone-api-request-unauthorized', xmlhttp) | ||
reject(xmlhttp) | ||
} else { | ||
this.dispatchEvent('ozone-api-request-error', xmlhttp) | ||
reject(xmlhttp) | ||
} | ||
} | ||
xmlhttp.onerror = handleResponse; | ||
xmlhttp.onload = handleResponse | ||
xmlhttp.send(this.body); | ||
}); | ||
return this; | ||
} | ||
xmlhttp.ontimeout = () => { | ||
this.dispatchEvent('ozone-api-request-timeout', xmlhttp) | ||
reject(xmlhttp) | ||
} | ||
/** | ||
* | ||
* @param {XMLHttpRequest} request (optional) This parameters overwrite the default XmlHttpRequest. | ||
* @return {Promise<XMLHttpRequest>} | ||
*/ | ||
sendRequest(request?: XMLHttpRequest):Promise<XMLHttpRequest>{ | ||
return this.send(request).result | ||
xmlhttp.onerror = handleResponse | ||
} | ||
xmlhttp.send(this.body) | ||
}) | ||
return this | ||
} | ||
/** | ||
* | ||
* @param {XMLHttpRequest} request (optional) This parameters overwrite the default XmlHttpRequest. | ||
* @return {Promise<XMLHttpRequest>} | ||
*/ | ||
sendRequest(request?: XMLHttpRequest): Promise<XMLHttpRequest> { | ||
return this.send(request).result | ||
} | ||
private dispatchEvent(eventName: string, xmlhttp: XMLHttpRequest) { | ||
if (this.eventTarget) { | ||
this.eventTarget.dispatchEvent(new CustomEvent<XMLHttpRequest>(eventName, { | ||
bubbles: true, detail: xmlhttp | ||
})) | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21209
593