Skip to content

Commit

Permalink
style: lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
deot committed Dec 15, 2023
1 parent bda09d4 commit c46f1b5
Show file tree
Hide file tree
Showing 49 changed files with 270 additions and 274 deletions.
37 changes: 18 additions & 19 deletions packages/client/__tests__/fixtures/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import formidable from 'formidable';

const createServer = async (port: number, host: string) => {
return new Promise((resolve) => {
let server = http
const server = http
.createServer(async (req, res) => {
// 处理CORS
res.setHeader('Access-Control-Allow-Origin', "*");
res.setHeader('Access-Control-Allow-Credentials', "*");
// 处理CORS
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', '*');

Expand Down Expand Up @@ -39,16 +39,16 @@ const createServer = async (port: number, host: string) => {
}

// 也可以用searchParams
let query = (url.parse(req.url!).query || '')
const query = (url.parse(req.url!).query || '')
.split('&')
.filter(i => !!i)
.reduce((pre, cur) => {
let [key, value] = cur.split('=');
const [key, value] = cur.split('=');

pre[key] = decodeURIComponent(value);
return pre;
}, {});

let body = {};

if (req.method === 'POST' || req.method === 'DELETE' || req.method === 'PUT') {
Expand All @@ -60,7 +60,7 @@ const createServer = async (port: number, host: string) => {
.then(([fields, files]) => {
let response = JSON.parse(fields?.response?.[0] || null);
if (!response && files && Object.keys(files).length) {
let file = files[Object.keys(files)[0]][0];
const file = files[Object.keys(files)[0]][0];
response = JSON.parse(fs.readFileSync(file.filepath).toString())?.response;
}
resolve$({ response });
Expand All @@ -69,18 +69,18 @@ const createServer = async (port: number, host: string) => {
resolve$({ response: { status: 0, body: e } });
});
return;
}
}

let postData = '';
req.on('data', chunk => {
req.on('data', (chunk) => {
postData += chunk;
});
req.on('end', async () => {
try {
resolve$(JSON.parse(postData.toString() || '{}'));
} catch (e) {
try {
let data = new URLSearchParams(postData.toString());
const data = new URLSearchParams(postData.toString());
resolve$({ response: JSON.parse(data.get('response')!) });
} catch {
resolve$({ response: { status: 0 } });
Expand All @@ -90,17 +90,17 @@ const createServer = async (port: number, host: string) => {
});
}

let {
delay = 0.1,
response = JSON.stringify({
method: req.method,
const {
delay = 0.1,
response = JSON.stringify({
method: req.method,
url: req.url,
reuqest: body
})
} = { ...query, ...body };

response = typeof response === 'string' ? response : JSON.stringify(response);
setTimeout(() => res.end(response), delay * 1000);
const response$ = typeof response === 'string' ? response : JSON.stringify(response);
setTimeout(() => res.end(response$), delay * 1000);
});

server.listen({ port, host }, () => {
Expand All @@ -127,11 +127,10 @@ export const impl = async () => {
return baseUrl;
};


if (typeof beforeAll === 'undefined') {
(async () => {
const { port, host, baseUrl } = await Server.available();
await createServer(port as number, host);
console.log(baseUrl);
})();
}
}
2 changes: 1 addition & 1 deletion packages/client/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ describe('index.ts', async () => {
expect(Network3.request.onRequest.length).toBe(1);
expect(Network3.request.onResponse.length).toBe(1);
});
});
});
10 changes: 5 additions & 5 deletions packages/client/__tests__/limit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('limit.ts', async () => {
const { body } = leaf.response!;

if (
!body
!body
|| (body && typeof body.status !== 'number')
) {
leaf.response!.statusText = `response.body must be a json.`;
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('limit.ts', async () => {
data: {}
}
};
const response = await Network.http(serverUrl, {
const response = await Network.http(serverUrl, {
method: 'POST',
body
});
Expand All @@ -64,7 +64,7 @@ describe('limit.ts', async () => {
}
};
try {
await Network.http(serverUrl, {
await Network.http(serverUrl, {
method: 'POST',
body
});
Expand All @@ -81,12 +81,12 @@ describe('limit.ts', async () => {
}
};
try {
await Network.http(serverUrl, {
await Network.http(serverUrl, {
method: 'POST',
body
});
} catch (response: any) {
expect(response.statusText).toBe('HTTP_CANCEL');
}
});
});
});
14 changes: 7 additions & 7 deletions packages/client/__tests__/native.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ describe.skip('request.ts', async () => {
const launch = E2E.impl();
const baseUrl = `file://${resolve(__dirname, './fixtures/native.html')}`;
const methods = [
'string',
'blob',
'file',
'formData',
'arrayBuffer',
'string',
'blob',
'file',
'formData',
'arrayBuffer',
'files',
'URLSearchParams'
];
Expand All @@ -28,7 +28,7 @@ describe.skip('request.ts', async () => {
task$ = task$
.then(() => operater.setValue(`#${id}`, api))
.then(() => page.waitForResponse(v => v.url() === api))
.then((e) => e.json())
.then(e => e.json())
.then((e) => {
expect(e).toEqual({ status: 1, data: {} });
});
Expand All @@ -38,4 +38,4 @@ describe.skip('request.ts', async () => {
return task;
}, Promise.resolve());
});
});
});
14 changes: 7 additions & 7 deletions packages/client/__tests__/provider-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('fetch.ts', async () => {
data: {}
}
};
const response = await Network.http(serverUrl, {
const response = await Network.http(serverUrl, {
timeout: 0,
method: 'POST',
body
Expand All @@ -47,7 +47,7 @@ describe('fetch.ts', async () => {
data: {}
}
};
const response = await Network.http(serverUrl, {
const response = await Network.http(serverUrl, {
method: 'DELETE',
body
});
Expand All @@ -67,7 +67,7 @@ describe('fetch.ts', async () => {
data: {}
}
};
const response = await Network.http(serverUrl, {
const response = await Network.http(serverUrl, {
method: 'PUT',
body
});
Expand All @@ -82,7 +82,7 @@ describe('fetch.ts', async () => {

it('timeout', async () => {
try {
await Network.http(serverUrl, {
await Network.http(serverUrl, {
method: 'PUT',
timeout: 100
});
Expand All @@ -93,7 +93,7 @@ describe('fetch.ts', async () => {

it('cancel', async () => {
try {
const leaf = Network.http(serverUrl, {
const leaf = Network.http(serverUrl, {
method: 'PUT',
});
setTimeout(() => leaf.cancel());
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('fetch.ts', async () => {
});

it('headers', async () => {
let headers = {};
const headers = {};
// eslint-disable-next-line no-proto
(headers as any).__proto__['Cookie'] = 'any';
(headers as any)['Cookies'] = '';
Expand All @@ -157,4 +157,4 @@ describe('fetch.ts', async () => {
expect(e.status).toBe(500);
}
});
});
});
2 changes: 1 addition & 1 deletion packages/client/__tests__/provider-jsonp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ describe('jsonp.ts', async () => {
(window as any)[jsonp]({});
}
});
});
});
14 changes: 7 additions & 7 deletions packages/client/__tests__/provider-xhr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('xhr.ts', async () => {
data: {}
}
};
const response = await Network.http(serverUrl, {
const response = await Network.http(serverUrl, {
method: 'POST',
body
});
Expand All @@ -48,7 +48,7 @@ describe('xhr.ts', async () => {
data: {}
}
};
const response = await Network.http(serverUrl, {
const response = await Network.http(serverUrl, {
method: 'DELETE',
body
});
Expand All @@ -68,7 +68,7 @@ describe('xhr.ts', async () => {
data: {}
}
};
const response = await Network.http(serverUrl, {
const response = await Network.http(serverUrl, {
method: 'PUT',
body
});
Expand All @@ -83,7 +83,7 @@ describe('xhr.ts', async () => {

it('timeout', async () => {
try {
await Network.http(serverUrl, {
await Network.http(serverUrl, {
method: 'PUT',
timeout: 100
});
Expand All @@ -94,7 +94,7 @@ describe('xhr.ts', async () => {

it('cancel', async () => {
try {
const leaf = Network.http(serverUrl, {
const leaf = Network.http(serverUrl, {
method: 'PUT',
});
setTimeout(() => leaf.cancel());
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('xhr.ts', async () => {
});

it('headers', async () => {
let headers = {};
const headers = {};
// eslint-disable-next-line no-proto
(headers as any).__proto__['Cookie'] = 'any';
(headers as any)['Cookies'] = '';
Expand All @@ -184,4 +184,4 @@ describe('xhr.ts', async () => {
}
});
});
});
});
21 changes: 10 additions & 11 deletions packages/client/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import type { HTTPProvider } from "@deot/http-core";
import { HTTPRequest, HTTPResponse, HTTPHeaders, HTTPShellLeaf, ERROR_CODE } from "@deot/http-core";
import type { HTTPProvider } from '@deot/http-core';
import { HTTPRequest, HTTPResponse, HTTPHeaders, HTTPShellLeaf, ERROR_CODE } from '@deot/http-core';

export const provider: HTTPProvider = (request: HTTPRequest, leaf: HTTPShellLeaf) => {
return new Promise((resolve, reject) => {
const { url, headers, body, credentials, method, timeout, responseType, ...fetchOptions } = request;


const controller = new AbortController();

let timer = timeout
let timer = timeout
? setTimeout(() => {
controller.abort();
onError(ERROR_CODE.HTTP_REQUEST_TIMEOUT);
}, timeout)
: null;
let response: Response;
let response: Response;

const getExtra = () => {
let headers$ = new HTTPHeaders();
const headers$ = new HTTPHeaders();
if (response) {
response.headers.forEach((v, k) => headers$.set(k, v, true));
}
Expand All @@ -37,9 +36,9 @@ export const provider: HTTPProvider = (request: HTTPRequest, leaf: HTTPShellLeaf
};

const onSuccess = (body$: any) => {
resolve(new HTTPResponse({
resolve(new HTTPResponse({
...getExtra(),
body: body$
body: body$
}));
timer && clearTimeout(timer);
timer = null;
Expand All @@ -60,7 +59,7 @@ export const provider: HTTPProvider = (request: HTTPRequest, leaf: HTTPShellLeaf
}).then((res) => {
response = res;
if (res.status >= 200 && res.status < 300) {
let fn = res[responseType || 'text'];
const fn = res[responseType || 'text'];
if (!fn) return onSuccess(res);
fn.call(res)
.then((data: any) => {
Expand All @@ -76,7 +75,7 @@ export const provider: HTTPProvider = (request: HTTPRequest, leaf: HTTPShellLeaf
return res;
}).catch((e) => {
onError(ERROR_CODE.HTTP_STATUS_ERROR, e);
// no throw again, avoid unhandled error
// no throw again, avoid unhandled error
});

// rebuild cancel
Expand All @@ -88,4 +87,4 @@ export const provider: HTTPProvider = (request: HTTPRequest, leaf: HTTPShellLeaf

leaf.server = fetch$;
});
};
};
Loading

0 comments on commit c46f1b5

Please sign in to comment.