@pikku/fetch 0.12.0 → 0.12.1
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/CHANGELOG.md +6 -0
- package/dist/cjs/core-pikku-fetch.d.ts +21 -0
- package/dist/cjs/core-pikku-fetch.js +30 -0
- package/dist/cjs/pikku-fetch.js +1 -1
- package/dist/esm/core-pikku-fetch.d.ts +21 -0
- package/dist/esm/core-pikku-fetch.js +29 -0
- package/dist/esm/pikku-fetch.js +1 -1
- package/package.json +3 -2
- package/src/core-pikku-fetch.ts +42 -0
- package/src/pikku-fetch.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
## 0.12.0
|
|
2
2
|
|
|
3
|
+
## 0.12.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 3fbd05c: Encode URI path parameters with encodeURIComponent to prevent path injection via user-supplied data values.
|
|
8
|
+
|
|
3
9
|
### New Features
|
|
4
10
|
|
|
5
11
|
- Auto-remove `Content-Type` header for bodyless requests (GET/DELETE)
|
|
@@ -61,6 +61,27 @@ export declare class CorePikkuFetch {
|
|
|
61
61
|
get(uri: string, data: any, options?: RequestInit): Promise<any>;
|
|
62
62
|
patch(uri: string, data: any, options?: RequestInit): Promise<any>;
|
|
63
63
|
head(uri: string, data: any, options?: RequestInit): Promise<any>;
|
|
64
|
+
/**
|
|
65
|
+
* Uploads a file to a URL obtained from `getUploadURL`.
|
|
66
|
+
* Handles both presigned URLs (e.g. S3) and header-based auth (e.g. B2).
|
|
67
|
+
*
|
|
68
|
+
* @param {Object} uploadInfo - The result from the backend's `getUploadURL` call.
|
|
69
|
+
* @param {string} uploadInfo.uploadUrl - The URL to upload to.
|
|
70
|
+
* @param {string} uploadInfo.assetKey - The finalized asset key.
|
|
71
|
+
* @param {Record<string, string>} [uploadInfo.uploadHeaders] - Optional headers required by the storage backend.
|
|
72
|
+
* @param {Blob | File | Buffer | ReadableStream} body - The file content to upload.
|
|
73
|
+
* @param {string} [contentType] - The MIME type (used as fallback if not in uploadHeaders).
|
|
74
|
+
* @returns {Promise<{ assetKey: string; response: Response }>} - The asset key and raw response.
|
|
75
|
+
*/
|
|
76
|
+
uploadFile(uploadInfo: {
|
|
77
|
+
uploadUrl: string;
|
|
78
|
+
assetKey: string;
|
|
79
|
+
uploadHeaders?: Record<string, string>;
|
|
80
|
+
uploadMethod?: 'PUT' | 'POST';
|
|
81
|
+
}, body: Blob | File | BufferSource | ReadableStream, contentType?: string): Promise<{
|
|
82
|
+
assetKey: string;
|
|
83
|
+
response: Response;
|
|
84
|
+
}>;
|
|
64
85
|
/**
|
|
65
86
|
* Makes an API request with the specified URI, method, and data, and optionally transforms dates in the response.
|
|
66
87
|
*
|
|
@@ -103,6 +103,36 @@ class CorePikkuFetch {
|
|
|
103
103
|
return yield this.api(uri, 'HEAD', data, options);
|
|
104
104
|
});
|
|
105
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Uploads a file to a URL obtained from `getUploadURL`.
|
|
108
|
+
* Handles both presigned URLs (e.g. S3) and header-based auth (e.g. B2).
|
|
109
|
+
*
|
|
110
|
+
* @param {Object} uploadInfo - The result from the backend's `getUploadURL` call.
|
|
111
|
+
* @param {string} uploadInfo.uploadUrl - The URL to upload to.
|
|
112
|
+
* @param {string} uploadInfo.assetKey - The finalized asset key.
|
|
113
|
+
* @param {Record<string, string>} [uploadInfo.uploadHeaders] - Optional headers required by the storage backend.
|
|
114
|
+
* @param {Blob | File | Buffer | ReadableStream} body - The file content to upload.
|
|
115
|
+
* @param {string} [contentType] - The MIME type (used as fallback if not in uploadHeaders).
|
|
116
|
+
* @returns {Promise<{ assetKey: string; response: Response }>} - The asset key and raw response.
|
|
117
|
+
*/
|
|
118
|
+
uploadFile(uploadInfo, body, contentType) {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
var _a;
|
|
121
|
+
const headers = Object.assign({}, uploadInfo.uploadHeaders);
|
|
122
|
+
if (!headers['Content-Type'] && contentType) {
|
|
123
|
+
headers['Content-Type'] = contentType;
|
|
124
|
+
}
|
|
125
|
+
const response = yield fetch(uploadInfo.uploadUrl, {
|
|
126
|
+
method: (_a = uploadInfo.uploadMethod) !== null && _a !== void 0 ? _a : 'PUT',
|
|
127
|
+
headers,
|
|
128
|
+
body: body,
|
|
129
|
+
});
|
|
130
|
+
if (response.status >= 400) {
|
|
131
|
+
throw response;
|
|
132
|
+
}
|
|
133
|
+
return { assetKey: uploadInfo.assetKey, response };
|
|
134
|
+
});
|
|
135
|
+
}
|
|
106
136
|
/**
|
|
107
137
|
* Makes an API request with the specified URI, method, and data, and optionally transforms dates in the response.
|
|
108
138
|
*
|
package/dist/cjs/pikku-fetch.js
CHANGED
|
@@ -28,7 +28,7 @@ const corePikkuFetch = (uri, data, options) => __awaiter(void 0, void 0, void 0,
|
|
|
28
28
|
const keys = Object.keys(data);
|
|
29
29
|
for (const key of keys) {
|
|
30
30
|
if (uri.includes(`:${key}`)) {
|
|
31
|
-
uri = uri.replace(`:${key}`, data[key]);
|
|
31
|
+
uri = uri.replace(`:${key}`, encodeURIComponent(String(data[key])));
|
|
32
32
|
delete data[key];
|
|
33
33
|
}
|
|
34
34
|
}
|
|
@@ -61,6 +61,27 @@ export declare class CorePikkuFetch {
|
|
|
61
61
|
get(uri: string, data: any, options?: RequestInit): Promise<any>;
|
|
62
62
|
patch(uri: string, data: any, options?: RequestInit): Promise<any>;
|
|
63
63
|
head(uri: string, data: any, options?: RequestInit): Promise<any>;
|
|
64
|
+
/**
|
|
65
|
+
* Uploads a file to a URL obtained from `getUploadURL`.
|
|
66
|
+
* Handles both presigned URLs (e.g. S3) and header-based auth (e.g. B2).
|
|
67
|
+
*
|
|
68
|
+
* @param {Object} uploadInfo - The result from the backend's `getUploadURL` call.
|
|
69
|
+
* @param {string} uploadInfo.uploadUrl - The URL to upload to.
|
|
70
|
+
* @param {string} uploadInfo.assetKey - The finalized asset key.
|
|
71
|
+
* @param {Record<string, string>} [uploadInfo.uploadHeaders] - Optional headers required by the storage backend.
|
|
72
|
+
* @param {Blob | File | Buffer | ReadableStream} body - The file content to upload.
|
|
73
|
+
* @param {string} [contentType] - The MIME type (used as fallback if not in uploadHeaders).
|
|
74
|
+
* @returns {Promise<{ assetKey: string; response: Response }>} - The asset key and raw response.
|
|
75
|
+
*/
|
|
76
|
+
uploadFile(uploadInfo: {
|
|
77
|
+
uploadUrl: string;
|
|
78
|
+
assetKey: string;
|
|
79
|
+
uploadHeaders?: Record<string, string>;
|
|
80
|
+
uploadMethod?: 'PUT' | 'POST';
|
|
81
|
+
}, body: Blob | File | BufferSource | ReadableStream, contentType?: string): Promise<{
|
|
82
|
+
assetKey: string;
|
|
83
|
+
response: Response;
|
|
84
|
+
}>;
|
|
64
85
|
/**
|
|
65
86
|
* Makes an API request with the specified URI, method, and data, and optionally transforms dates in the response.
|
|
66
87
|
*
|
|
@@ -84,6 +84,35 @@ export class CorePikkuFetch {
|
|
|
84
84
|
async head(uri, data, options) {
|
|
85
85
|
return await this.api(uri, 'HEAD', data, options);
|
|
86
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Uploads a file to a URL obtained from `getUploadURL`.
|
|
89
|
+
* Handles both presigned URLs (e.g. S3) and header-based auth (e.g. B2).
|
|
90
|
+
*
|
|
91
|
+
* @param {Object} uploadInfo - The result from the backend's `getUploadURL` call.
|
|
92
|
+
* @param {string} uploadInfo.uploadUrl - The URL to upload to.
|
|
93
|
+
* @param {string} uploadInfo.assetKey - The finalized asset key.
|
|
94
|
+
* @param {Record<string, string>} [uploadInfo.uploadHeaders] - Optional headers required by the storage backend.
|
|
95
|
+
* @param {Blob | File | Buffer | ReadableStream} body - The file content to upload.
|
|
96
|
+
* @param {string} [contentType] - The MIME type (used as fallback if not in uploadHeaders).
|
|
97
|
+
* @returns {Promise<{ assetKey: string; response: Response }>} - The asset key and raw response.
|
|
98
|
+
*/
|
|
99
|
+
async uploadFile(uploadInfo, body, contentType) {
|
|
100
|
+
const headers = {
|
|
101
|
+
...uploadInfo.uploadHeaders,
|
|
102
|
+
};
|
|
103
|
+
if (!headers['Content-Type'] && contentType) {
|
|
104
|
+
headers['Content-Type'] = contentType;
|
|
105
|
+
}
|
|
106
|
+
const response = await fetch(uploadInfo.uploadUrl, {
|
|
107
|
+
method: uploadInfo.uploadMethod ?? 'PUT',
|
|
108
|
+
headers,
|
|
109
|
+
body: body,
|
|
110
|
+
});
|
|
111
|
+
if (response.status >= 400) {
|
|
112
|
+
throw response;
|
|
113
|
+
}
|
|
114
|
+
return { assetKey: uploadInfo.assetKey, response };
|
|
115
|
+
}
|
|
87
116
|
/**
|
|
88
117
|
* Makes an API request with the specified URI, method, and data, and optionally transforms dates in the response.
|
|
89
118
|
*
|
package/dist/esm/pikku-fetch.js
CHANGED
|
@@ -15,7 +15,7 @@ export const corePikkuFetch = async (uri, data, options) => {
|
|
|
15
15
|
const keys = Object.keys(data);
|
|
16
16
|
for (const key of keys) {
|
|
17
17
|
if (uri.includes(`:${key}`)) {
|
|
18
|
-
uri = uri.replace(`:${key}`, data[key]);
|
|
18
|
+
uri = uri.replace(`:${key}`, encodeURIComponent(String(data[key])));
|
|
19
19
|
delete data[key];
|
|
20
20
|
}
|
|
21
21
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pikku/fetch",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"author": "yasser.fadl@gmail.com",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"release": "yarn build && npm test",
|
|
22
22
|
"test": "bash run-tests.sh",
|
|
23
23
|
"test:watch": "bash run-tests.sh --watch",
|
|
24
|
-
"test:coverage": "bash run-tests.sh --coverage"
|
|
24
|
+
"test:coverage": "bash run-tests.sh --coverage",
|
|
25
|
+
"prepublishOnly": "yarn build"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"typescript": "^5.9"
|
package/src/core-pikku-fetch.ts
CHANGED
|
@@ -114,6 +114,48 @@ export class CorePikkuFetch {
|
|
|
114
114
|
return await this.api(uri, 'HEAD', data, options)
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Uploads a file to a URL obtained from `getUploadURL`.
|
|
119
|
+
* Handles both presigned URLs (e.g. S3) and header-based auth (e.g. B2).
|
|
120
|
+
*
|
|
121
|
+
* @param {Object} uploadInfo - The result from the backend's `getUploadURL` call.
|
|
122
|
+
* @param {string} uploadInfo.uploadUrl - The URL to upload to.
|
|
123
|
+
* @param {string} uploadInfo.assetKey - The finalized asset key.
|
|
124
|
+
* @param {Record<string, string>} [uploadInfo.uploadHeaders] - Optional headers required by the storage backend.
|
|
125
|
+
* @param {Blob | File | Buffer | ReadableStream} body - The file content to upload.
|
|
126
|
+
* @param {string} [contentType] - The MIME type (used as fallback if not in uploadHeaders).
|
|
127
|
+
* @returns {Promise<{ assetKey: string; response: Response }>} - The asset key and raw response.
|
|
128
|
+
*/
|
|
129
|
+
public async uploadFile(
|
|
130
|
+
uploadInfo: {
|
|
131
|
+
uploadUrl: string
|
|
132
|
+
assetKey: string
|
|
133
|
+
uploadHeaders?: Record<string, string>
|
|
134
|
+
uploadMethod?: 'PUT' | 'POST'
|
|
135
|
+
},
|
|
136
|
+
body: Blob | File | BufferSource | ReadableStream,
|
|
137
|
+
contentType?: string
|
|
138
|
+
): Promise<{ assetKey: string; response: Response }> {
|
|
139
|
+
const headers: Record<string, string> = {
|
|
140
|
+
...uploadInfo.uploadHeaders,
|
|
141
|
+
}
|
|
142
|
+
if (!headers['Content-Type'] && contentType) {
|
|
143
|
+
headers['Content-Type'] = contentType
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const response = await fetch(uploadInfo.uploadUrl, {
|
|
147
|
+
method: uploadInfo.uploadMethod ?? 'PUT',
|
|
148
|
+
headers,
|
|
149
|
+
body: body as any,
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
if (response.status >= 400) {
|
|
153
|
+
throw response
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return { assetKey: uploadInfo.assetKey, response }
|
|
157
|
+
}
|
|
158
|
+
|
|
117
159
|
/**
|
|
118
160
|
* Makes an API request with the specified URI, method, and data, and optionally transforms dates in the response.
|
|
119
161
|
*
|
package/src/pikku-fetch.ts
CHANGED
|
@@ -21,7 +21,7 @@ export const corePikkuFetch = async (
|
|
|
21
21
|
const keys = Object.keys(data)
|
|
22
22
|
for (const key of keys) {
|
|
23
23
|
if (uri.includes(`:${key}`)) {
|
|
24
|
-
uri = uri.replace(`:${key}`, data[key])
|
|
24
|
+
uri = uri.replace(`:${key}`, encodeURIComponent(String(data[key])))
|
|
25
25
|
delete data[key]
|
|
26
26
|
}
|
|
27
27
|
}
|