-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocal-filesystem-api-lib.js
More file actions
273 lines (252 loc) · 8.4 KB
/
Copy pathlocal-filesystem-api-lib.js
File metadata and controls
273 lines (252 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* Filesystem API Library
*
* A collection of reusable functions for interacting with the local filesystem API.
* This library provides a clean interface for common local filesystem operations
* like reading, writing, and managing files and directories through REST endpoints.
*
* GitHub: https://github.com/postman-solutions-eng/postman-local-file-access
*
* This library is part of a Node.js application that exposes local filesystem
* folder access through a REST API. It's specifically designed for use in Postman
* pre- and post-request scripts that need to manipulate local files.
*
* Key Features:
* - Secure authentication with token
* - CRUD operations for files and directories
* - Path traversal protection
* - Support for binary and text files
* - Directory listing and management
*
* Prerequisites:
* - Requires a running instance of the Filesystem API server
* - Valid authentication token
* - Postman environment variables:
* - baseUrl: The base URL of your Filesystem API server
* - token: Your authentication token (optional, can be passed directly to functions)
*
* Usage Example:
* ```javascript
* const fsLib = pm.require('@postman-solutions-eng/local-filesystem-api-lib');
*
* // Setup authentication
* await fsLib.setupAuth('your-token');
*
* // Read a file
* const content = await fsLib.readFile('path/to/file');
* ```
*
* For complete documentation and examples, visit:
* https://github.com/postman-solutions-eng/postman-local-file-access
*
*/
/**
* Get authentication token from parameter or environment
* @param {string} [token] - Optional token parameter
* @returns {string} The token to use
* @throws {Error} If no token is available
*/
function getToken(token) {
const envToken = pm.variables.get('token');
if (!token && !envToken) {
throw new Error('No token provided and no token found in environment');
}
return token || envToken;
}
/**
* Setup authentication token
* @param {string} [token] - Optional authentication token
* @returns {Promise<Object>} Response from the auth setup endpoint
*/
async function setupAuth(token) {
const authToken = getToken(token);
const response = await pm.sendRequest({
url: `${pm.variables.get('baseUrl')}/api/auth/setup`,
method: 'POST',
header: { 'Content-Type': 'application/json' },
body: { mode: 'raw', raw: JSON.stringify({ token: authToken }) }
});
if (response.code !== 200) {
throw new Error(`Auth setup failed: ${response.json().error}`);
}
return response.json();
}
/**
* Read file contents
* @param {string} path - Path to the file
* @param {string} [token] - Optional authentication token
* @returns {Promise<string>} File contents
*/
async function readFile(path, token) {
const authToken = getToken(token);
const response = await pm.sendRequest({
url: `${pm.variables.get('baseUrl')}/api/files/${path}`,
method: 'GET',
header: { 'Authorization': `Bearer ${authToken}` }
});
if (response.code !== 200) {
throw new Error(`Read file failed: ${response.json().error}`);
}
return response.text();
}
/**
* Create a new file
* @param {string} path - Path where to create the file
* @param {string} content - File content
* @param {string} [token] - Optional authentication token
* @returns {Promise<Object>} Response from create file endpoint
*/
async function createFile(path, content, token) {
const authToken = getToken(token);
const response = await pm.sendRequest({
url: `${pm.variables.get('baseUrl')}/api/files/${path}`,
method: 'POST',
header: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: { content }
}
});
if (response.code !== 200) {
throw new Error(`Create file failed: ${response.json().error}`);
}
return response.json();
}
/**
* Update file contents
* @param {string} path - Path to the file
* @param {string} content - New file content
* @param {string} [token] - Optional authentication token
* @returns {Promise<Object>} Response from update file endpoint
*/
async function updateFile(path, content, token) {
const authToken = getToken(token);
const response = await pm.sendRequest({
url: `${pm.variables.get('baseUrl')}/api/files/${path}`,
method: 'PUT',
header: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: { content }
}
});
if (response.code !== 200) {
throw new Error(`Update file failed: ${response.json().error}`);
}
return response.json();
}
/**
* Delete a file
* @param {string} path - Path to the file to delete
* @param {string} [token] - Optional authentication token
* @returns {Promise<Object>} Response from delete file endpoint
*/
async function deleteFile(path, token) {
const authToken = getToken(token);
const response = await pm.sendRequest({
url: `${pm.variables.get('baseUrl')}/api/files/${path}`,
method: 'DELETE',
header: { 'Authorization': `Bearer ${authToken}` }
});
if (response.code !== 200) {
throw new Error(`Delete file failed: ${response.json().error}`);
}
return response.json();
}
/**
* List directory contents
* @param {string} [path=''] - Path to the directory
* @param {string} [token] - Optional authentication token
* @returns {Promise<Array>} Array of directory contents with metadata
*/
async function listDirectory(path = '', token) {
const authToken = getToken(token);
const response = await pm.sendRequest({
url: `${pm.variables.get('baseUrl')}/api/directories/${path}`,
method: 'GET',
header: { 'Authorization': `Bearer ${authToken}` }
});
if (response.code !== 200) {
throw new Error(`List directory failed: ${response.json().error}`);
}
return response.json();
}
/**
* Create a new directory
* @param {string} path - Path where to create the directory
* @param {string} [token] - Optional authentication token
* @returns {Promise<Object>} Response from create directory endpoint
*/
async function createDirectory(path, token) {
const authToken = getToken(token);
const response = await pm.sendRequest({
url: `${pm.variables.get('baseUrl')}/api/directories/${path}`,
method: 'POST',
header: { 'Authorization': `Bearer ${authToken}` }
});
if (response.code !== 200) {
throw new Error(`Create directory failed: ${response.json().error}`);
}
return response.json();
}
/**
* Delete a directory
* @param {string} path - Path to the directory to delete
* @param {string} [token] - Optional authentication token
* @returns {Promise<Object>} Response from delete directory endpoint
*/
async function deleteDirectory(path, token) {
const authToken = getToken(token);
const response = await pm.sendRequest({
url: `${pm.variables.get('baseUrl')}/api/directories/${path}`,
method: 'DELETE',
header: { 'Authorization': `Bearer ${authToken}` }
});
if (response.code !== 200) {
throw new Error(`Delete directory failed: ${response.json().error}`);
}
return response.json();
}
/**
* Upload a file
* @param {string} path - Path where to upload the file
* @param {string} fileData - The file data (base64 encoded in the format data:application/octet-stream;base64,...) or raw content
* @param {string} [token] - Optional authentication token
* @returns {Promise<Object>} Response from upload file endpoint
*/
async function uploadFile(path, fileData, token) {
const authToken = getToken(token);
const response = await pm.sendRequest({
url: `${pm.variables.get('baseUrl')}/api/files/${path}`,
method: 'POST',
header: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: { fileData }
}
});
if (response.code !== 200) {
throw new Error(`Upload file failed: ${response.json().error}`);
}
return response.json();
}
module.exports = {
setupAuth,
readFile,
createFile,
updateFile,
deleteFile,
listDirectory,
createDirectory,
deleteDirectory,
uploadFile
};