-
Notifications
You must be signed in to change notification settings - Fork 28
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
2686d55
commit 0f3b245
Showing
16 changed files
with
148 additions
and
44 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
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
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,31 @@ | ||
--- | ||
layout: layout | ||
title: Util | ||
type: page | ||
nav: nav | ||
class: style-api style-api-detail | ||
--- | ||
|
||
# Util | ||
|
||
## FakeXMLHttpRequest | ||
|
||
Make a fake XHR object that doesn't send a request. | ||
|
||
```ts | ||
import Pjax, { FakeXMLHttpRequest } from 'pjax-api'; | ||
|
||
new Pjax({ | ||
fetch: { | ||
rewrite: url => | ||
FakeXMLHttpRequest.create( | ||
url, | ||
fetch(url, { headers: { 'Content-Type': 'application/json' } }) | ||
.then(res => res.json()) | ||
.then(data => | ||
new DOMParser().parseFromString( | ||
`<title>${data.title}</title><body>${data.body}</body>`, | ||
'text/html'))), | ||
}, | ||
}); | ||
``` |
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
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 +1,2 @@ | ||
export { GUI as Pjax, GUI as default } from './layer/interface/service/gui'; | ||
export { FakeXMLHttpRequest } from './lib/xhr'; |
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
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,58 @@ | ||
import { AtomicPromise } from 'spica/promise'; | ||
|
||
export class FakeXMLHttpRequest extends XMLHttpRequest { | ||
public static create(url: string, response: Document | PromiseLike<Document>): FakeXMLHttpRequest { | ||
const xhr = new FakeXMLHttpRequest(); | ||
AtomicPromise.resolve(response) | ||
.then(response => { | ||
Object.defineProperties(xhr, { | ||
responseURL: { | ||
value: url, | ||
}, | ||
responseXML: { | ||
value: response, | ||
}, | ||
}); | ||
xhr.send(); | ||
}); | ||
return xhr; | ||
} | ||
constructor() { | ||
super(); | ||
this.responseType = 'document'; | ||
} | ||
public override send(_?: Document | XMLHttpRequestBodyInit | null | undefined): void { | ||
let state = 3; | ||
Object.defineProperties(this, { | ||
readyState: { | ||
get: () => state, | ||
}, | ||
status: { | ||
value: 200, | ||
}, | ||
statusText: { | ||
value: 'OK', | ||
}, | ||
response: { | ||
get: () => | ||
this.responseType === 'document' | ||
? this.responseXML | ||
: this.responseText, | ||
}, | ||
}) | ||
setTimeout(() => { | ||
this.dispatchEvent(new ProgressEvent('loadstart')); | ||
state = 4; | ||
this.dispatchEvent(new ProgressEvent('loadend')); | ||
this.dispatchEvent(new ProgressEvent('load')); | ||
}); | ||
} | ||
public override getResponseHeader(name: string): string | null { | ||
switch (name.toLowerCase()) { | ||
case 'content-type': | ||
return 'text/html'; | ||
default: | ||
return null; | ||
} | ||
} | ||
} |
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
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