From bfa21ce2b13daf0135b13eefbc3348b89f0fe7a7 Mon Sep 17 00:00:00 2001 From: SilverMira <66930495+SilverMira@users.noreply.github.com> Date: Fri, 12 May 2023 20:55:33 +0800 Subject: [PATCH] feat: setFetchFunction for web --- templates/web/src/client.ts.twig | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/templates/web/src/client.ts.twig b/templates/web/src/client.ts.twig index f952c31c2..095cb06ed 100644 --- a/templates/web/src/client.ts.twig +++ b/templates/web/src/client.ts.twig @@ -94,6 +94,7 @@ class Client { config = { endpoint: '{{ spec.endpoint }}', endpointRealtime: '', + fetchFunction: fetch, {% for header in spec.global.headers %} {{ header.key | caseLower }}: '', {% endfor %} @@ -136,6 +137,19 @@ class Client { return this; } + + /** + * Set Fetch Function + * + * @param {typeof fetch} fetchFunction + * + * @returns {this} + */ + setFetchFunction(fetchFunction: typeof fetch): this { + this.config.fetchFunction = fetchFunction; + + return this; + } {% for header in spec.global.headers %} /** @@ -383,7 +397,7 @@ class Client { try { let data = null; - const response = await fetch(url.toString(), options); + const response = await this.config.fetchFunction(url.toString(), options); if (response.headers.get('content-type')?.includes('application/json')) { data = await response.json();