@selkirk-systems/fetch 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/dist/Download.js +4 -8
- package/dist/FetchWrapper.js +389 -0
- package/dist/constants/ErrorConstants.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/middleware/FetchErrorHandler.js +37 -0
- package/dist/middleware/FetchLogger.js +29 -0
- package/lib/Download.js +175 -0
- package/lib/FetchWrapper.js +515 -0
- package/lib/index.js +3 -0
- package/{middleware → lib/middleware}/FetchErrorHandler.js +1 -1
- package/package.json +39 -19
- package/.babelrc +0 -6
- package/dist/Fetch.js +0 -463
- package/index.js +0 -2
- package/middleware/FetchValidationHandler.js +0 -22
- package/tests/Fetch.test.js +0 -114
- package/tests/files/test.pdf +0 -0
- package/tests/mocks/handlers.js +0 -75
- package/tests/mocks/server.js +0 -7
- /package/{constants → lib/constants}/ErrorConstants.js +0 -0
- /package/{middleware → lib/middleware}/FetchLogger.js +0 -0
package/tests/Fetch.test.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { server } from "./mocks/server";
|
|
2
|
-
import Fetch from "../src/Fetch";
|
|
3
|
-
|
|
4
|
-
beforeAll( () => server.listen() )
|
|
5
|
-
|
|
6
|
-
afterEach( () => server.resetHandlers() );
|
|
7
|
-
|
|
8
|
-
afterAll( () => server.close() )
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
test( "Fetch: Errors are swallowed", async ()=>{
|
|
12
|
-
|
|
13
|
-
jest.spyOn( console, 'error' ).mockImplementation( () => {} );
|
|
14
|
-
|
|
15
|
-
let errorOccured = false;
|
|
16
|
-
|
|
17
|
-
return Fetch( 'api/error',{method:"POST"} )
|
|
18
|
-
.then( ()=>{
|
|
19
|
-
expect( errorOccured ).toBeFalsy();
|
|
20
|
-
} )
|
|
21
|
-
|
|
22
|
-
} );
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
test( "Fetch: Adding catch causes errors to bubble",async() => {
|
|
26
|
-
|
|
27
|
-
jest.spyOn( console, 'error' ).mockImplementation( () => {} );
|
|
28
|
-
|
|
29
|
-
let error = null;
|
|
30
|
-
|
|
31
|
-
return Fetch( 'api/error',{method:"POST"} )
|
|
32
|
-
.catch( ( err )=>{
|
|
33
|
-
|
|
34
|
-
error = err;
|
|
35
|
-
return err;
|
|
36
|
-
|
|
37
|
-
} )
|
|
38
|
-
.finally( ( err )=>{
|
|
39
|
-
|
|
40
|
-
expect( error ).toBeTruthy();
|
|
41
|
-
expect(error.data).toBeTruthy();
|
|
42
|
-
expect(error.status).toBeTruthy();
|
|
43
|
-
expect(error.request).toBeTruthy();
|
|
44
|
-
expect(error.response).toBeTruthy();
|
|
45
|
-
|
|
46
|
-
} )
|
|
47
|
-
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test("Fetch: error on query param in url string", async()=>{
|
|
51
|
-
|
|
52
|
-
jest.spyOn( console, 'error' ).mockImplementation( () => {} );
|
|
53
|
-
|
|
54
|
-
let error = false;
|
|
55
|
-
|
|
56
|
-
return Fetch( 'api/error?name=severe',{method:"POST"} )
|
|
57
|
-
.catch( ( err )=>{
|
|
58
|
-
|
|
59
|
-
error = err;
|
|
60
|
-
return null;
|
|
61
|
-
|
|
62
|
-
} )
|
|
63
|
-
.finally(()=>{
|
|
64
|
-
|
|
65
|
-
expect( error ).toBeTruthy();
|
|
66
|
-
expect(error.data.type).toEqual("INVALID_URL");
|
|
67
|
-
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test("Fetch: response contains, req, res, data properties",async()=>{
|
|
73
|
-
|
|
74
|
-
jest.spyOn( console, 'error' ).mockImplementation( () => {} );
|
|
75
|
-
|
|
76
|
-
const [response] = await Fetch('api/users');
|
|
77
|
-
|
|
78
|
-
expect(response).toBeTruthy();
|
|
79
|
-
expect(response.request).toBeTruthy();
|
|
80
|
-
expect(response.response).toBeTruthy();
|
|
81
|
-
expect(response.data.length).toEqual(2);
|
|
82
|
-
expect(response.data).toContainEqual({name:"Kyle Bishop"});
|
|
83
|
-
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test("Fetch: Can abort request", async()=>{
|
|
87
|
-
|
|
88
|
-
jest.spyOn( console, 'error' ).mockImplementation( () => {} );
|
|
89
|
-
|
|
90
|
-
let called = 0;
|
|
91
|
-
const requestCount = 3;
|
|
92
|
-
const requestArray = [];
|
|
93
|
-
|
|
94
|
-
function handleResponse([response, aborted]){
|
|
95
|
-
if(aborted) return;
|
|
96
|
-
called++;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
for (let i = 0; i < requestCount; i++) {
|
|
100
|
-
requestArray.push(
|
|
101
|
-
Fetch('api/users').then(handleResponse)
|
|
102
|
-
)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return (
|
|
106
|
-
Promise.all(requestArray)
|
|
107
|
-
.finally(()=>{
|
|
108
|
-
|
|
109
|
-
expect(called).toEqual(1);
|
|
110
|
-
|
|
111
|
-
})
|
|
112
|
-
)
|
|
113
|
-
|
|
114
|
-
});
|
package/tests/files/test.pdf
DELETED
|
Binary file
|
package/tests/mocks/handlers.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
// Documentation: https://mswjs.io/docs/getting-started
|
|
3
|
-
|
|
4
|
-
import { rest } from "msw";
|
|
5
|
-
import * as path from 'path'
|
|
6
|
-
import * as fs from 'fs'
|
|
7
|
-
|
|
8
|
-
export const handlers = [
|
|
9
|
-
//Example
|
|
10
|
-
// rest.post("/login", (req, res, ctx) => {
|
|
11
|
-
// // Persist user's authentication in the session
|
|
12
|
-
// sessionStorage.setItem("is-authenticated", "true");
|
|
13
|
-
// return res(
|
|
14
|
-
// // Respond with a 200 status code
|
|
15
|
-
// ctx.status(200)
|
|
16
|
-
// );
|
|
17
|
-
// }),
|
|
18
|
-
|
|
19
|
-
//Example
|
|
20
|
-
// rest.get("/user", (req, res, ctx) => {
|
|
21
|
-
// // Check if the user is authenticated in this session
|
|
22
|
-
// const isAuthenticated = sessionStorage.getItem("is-authenticated");
|
|
23
|
-
// if (!isAuthenticated) {
|
|
24
|
-
// // If not authenticated, respond with a 403 error
|
|
25
|
-
// return res(
|
|
26
|
-
// ctx.status(403),
|
|
27
|
-
// ctx.json({
|
|
28
|
-
// errorMessage: "Not authorized",
|
|
29
|
-
// })
|
|
30
|
-
// );
|
|
31
|
-
// }
|
|
32
|
-
// // If authenticated, return a mocked user details
|
|
33
|
-
// return res(
|
|
34
|
-
// ctx.status(200),
|
|
35
|
-
// ctx.json({
|
|
36
|
-
// username: "admin",
|
|
37
|
-
// })
|
|
38
|
-
// );
|
|
39
|
-
// }),
|
|
40
|
-
|
|
41
|
-
rest.get('api/pdf', (_, res, ctx) => {
|
|
42
|
-
// Read the image from the file system using the "fs" module.
|
|
43
|
-
const fileBuffer = fs.readFileSync(
|
|
44
|
-
path.resolve(__dirname, '../files/test.pdf'),
|
|
45
|
-
)
|
|
46
|
-
return res(
|
|
47
|
-
ctx.set('Content-Length', fileBuffer.byteLength.toString()),
|
|
48
|
-
ctx.set('Content-Type', 'application/pdf'),
|
|
49
|
-
// Respond with the "ArrayBuffer".
|
|
50
|
-
ctx.body(fileBuffer),
|
|
51
|
-
)
|
|
52
|
-
}),
|
|
53
|
-
|
|
54
|
-
rest.post("api/login",(req,res,ctx)=>{
|
|
55
|
-
return res(ctx.status(200));
|
|
56
|
-
}),
|
|
57
|
-
|
|
58
|
-
rest.post("api/error",(req,res,ctx)=>{
|
|
59
|
-
return res(ctx.status(500));
|
|
60
|
-
}),
|
|
61
|
-
|
|
62
|
-
rest.get("api/users",(req,res,ctx)=>{
|
|
63
|
-
return res(
|
|
64
|
-
ctx.status(200),
|
|
65
|
-
ctx.json([
|
|
66
|
-
{
|
|
67
|
-
name:"Kyle Bishop",
|
|
68
|
-
},
|
|
69
|
-
{
|
|
70
|
-
name:"Jane Austin"
|
|
71
|
-
}
|
|
72
|
-
])
|
|
73
|
-
)
|
|
74
|
-
})
|
|
75
|
-
];
|
package/tests/mocks/server.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
// Documentation: https://mswjs.io/docs/getting-started
|
|
2
|
-
|
|
3
|
-
import { setupServer } from 'msw/node';
|
|
4
|
-
import { handlers } from './handlers';
|
|
5
|
-
|
|
6
|
-
// This configures a request mocking server with the given request handlers.
|
|
7
|
-
export const server = setupServer(...handlers)
|
|
File without changes
|
|
File without changes
|