Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
falsandtru committed Sep 7, 2023
1 parent 065119a commit 2757dd5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
4 changes: 2 additions & 2 deletions gh-pages/docs/apis/pjax/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Set a dictionary object having has/get/set/delete methods of Map to pass the doc

## fetch: {...} = ...

### rewrite: (url: string, method: string, headers: Headers, timeout: number, body: FormData | null) => XMLHttpRequest | undefined
### rewrite?: (url: string, method: string, headers: Headers, timeout: number, body: FormData | null) => XMLHttpRequest | undefined

Rewrite the XHR object, or replace it with another or fake.

Expand All @@ -79,7 +79,7 @@ Wait for the specified milliseconds after sending a request.

## update: {...} = ...

### rewrite: (url: string, document: Document, area: string, cache?: Document) => void = `() => undefined`
### rewrite?: (url: string, document: Document, area: string, cache?: Document) => void

Rewrite the source document object.
If you use the sequence option, you should use only it instead of this.
Expand Down
2 changes: 1 addition & 1 deletion src/layer/domain/data/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Unit: layer/domain/data/config', () => {
it('ignore', () => {
assert(new Config({}).update.ignore === '[href^="chrome-extension://"],[src*=".scr.kaspersky-labs.com/"]');
assert(new Config(new Config({})).update.ignore === '[href^="chrome-extension://"],[src*=".scr.kaspersky-labs.com/"]');
assert(new Config({ update: { ignore: 'style' } }).update.ignore === '[href^="chrome-extension://"],[src*=".scr.kaspersky-labs.com/"],style');
assert(new Config({ update: { ignore: 'style' } }).update.ignore === 'style,[href^="chrome-extension://"],[src*=".scr.kaspersky-labs.com/"]');
});

});
Expand Down
23 changes: 7 additions & 16 deletions src/layer/domain/data/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,16 @@ export { scope } from './config/scope';

export class Config implements Option {
constructor(option: Option) {
Object.defineProperties(this.update, {
ignore: {
enumerable: false,
set(this: Config['update'], value: string) {
this.ignores['_'] = value;
},
get(this: Config['update']): string {
return Object.keys(this.ignores)
.map(i => this.ignores[i])
.filter(s => s.trim().length > 0)
.join(',');
},
},
});
extend<Option>(this, option);
this.update.ignores.$ ??= option.update?.ignore ?? '';
this.update.ignore = Object.values(this.update.ignores).filter(s => s).join(',');
overwrite(this.scope, option?.scope ?? {});
this.fetch.headers = new Headers(this.fetch.headers);
Object.freeze(this);
this.fetch.headers.set('X-Requested-With', 'XMLHttpRequest');
this.fetch.headers.set('X-Pjax', '1');
Object.freeze(this);
Object.freeze(this.fetch);
Object.freeze(this.update);
}
public readonly areas = ['body'];
public readonly link = ':is(a, area)[href]:not([target])';
Expand All @@ -54,12 +44,13 @@ export class Config implements Option {
wait: 0,
};
public readonly update = {
rewrite: (_url: string, _document: Document, _area: string, _cache: Document | undefined): void => undefined,
rewrite: undefined as NonNullable<Option['update']>['rewrite'],
head: 'base, meta, link',
css: true,
script: true,
ignore: '',
ignores: {
$: undefined as string | undefined,
extension: '[href^="chrome-extension://"]',
security: '[src*=".scr.kaspersky-labs.com/"]',
},
Expand Down
2 changes: 1 addition & 1 deletion src/layer/domain/router/module/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function update(
const memory = event.type === RouterEventType.Popstate
? config.memory?.get(event.location.dest.path)
: undefined;
config.update.rewrite(
config.update.rewrite?.(
event.location.dest.href,
documents.src,
area,
Expand Down

0 comments on commit 2757dd5

Please sign in to comment.