Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
fix(fetch): compatible with new logger api
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Oct 23, 2023
1 parent f867317 commit a51d7d2
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions packages/fetch/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createLogger, globalAlwatr, NODE_MODE} from '@alwatr/logger';
import {createLogger, definePackage, NODE_MODE} from '@alwatr/logger';
import {getClientId, delay} from '@alwatr/util';

import type {FetchOptions} from './type.js';
Expand All @@ -8,10 +8,8 @@ export type * from './type.js';

const logger = createLogger('alwatr/fetch');

globalAlwatr.registeredList.push({
name: '@alwatr/fetch',
version: _ALWATR_VERSION_,
});
definePackage('fetch', '2.x');


let alwatrCacheStorage: Cache;
const cacheSupported = 'caches' in globalThis;
Expand Down Expand Up @@ -67,11 +65,11 @@ export async function serviceRequest<T extends AlwatrServiceResponse = AlwatrSer

if (responseJson.ok !== true) {
if (typeof responseJson.errorCode === 'string') {
logger.accident('serviceRequest', responseJson.errorCode, 'fetch response not ok', {responseJson});
logger.accident('serviceRequest', responseJson.errorCode, {responseJson});
throw new Error(responseJson.errorCode);
}
else {
logger.error('serviceRequest', 'fetch_nok', 'fetch response not ok', {responseJson});
logger.error('serviceRequest', 'fetch_nok', {responseJson});
throw new Error('fetch_nok');
}
}
Expand Down Expand Up @@ -119,7 +117,7 @@ function _processOptions(options: FetchOptions): Required<FetchOptions> {
options.headers ??= {};

if (options.cacheStrategy !== 'network_only' && cacheSupported !== true) {
logger.incident?.('fetch', 'fetch_cache_strategy_ignore', 'Cache storage not support in this browser', {
logger.incident?.('fetch', 'fetch_cache_strategy_unsupported', {
cacheSupported,
});
options.cacheStrategy = 'network_only';
Expand Down Expand Up @@ -196,7 +194,6 @@ async function _handleCacheStrategy(options: Required<FetchOptions>): Promise<Re
logger.accident(
'_handleCacheStrategy',
'fetch_cache_not_found',
'cacheStorage is cache_only but no cache found',
{url: request.url},
);
throw new Error('fetch_cache_not_found');
Expand Down Expand Up @@ -305,7 +302,7 @@ async function _handleRetryPattern(options: Required<FetchOptions>): Promise<Res
throw new Error('fetch_server_error');
}
catch (err) {
logger.accident('fetch', 'fetch_failed_retry', (err as Error)?.message || 'fetch failed and retry', err);
logger.accident('fetch', 'fetch_failed_retry', err);

if (globalThis.navigator?.onLine === false) {
throw new Error('offline');
Expand Down Expand Up @@ -344,7 +341,7 @@ function _handleTimeout(options: FetchOptions): Promise<Response> {
}

// abortController.signal.addEventListener('abort', () => {
// logger.incident('fetch', 'fetch_abort_signal', 'fetch abort signal received', {
// logger.incident('fetch', 'fetch_abort_signal', {
// reason: abortController.signal.reason,
// });
// });
Expand Down

0 comments on commit a51d7d2

Please sign in to comment.