microsoft-onedrive-mock 1.0.4 → 1.0.5

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.
Files changed (2) hide show
  1. package/dist/index.js +33 -8
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -52,25 +52,36 @@ const createApp = (config = {}) => {
52
52
  }
53
53
  next();
54
54
  }));
55
- app.use(express_1.default.json({
55
+ const rawParser = express_1.default.raw({
56
+ type: '*/*',
57
+ limit: '50mb',
56
58
  verify: (req, res, buf) => {
57
59
  req.rawBody = buf;
58
60
  }
59
- }));
60
- app.use(express_1.default.text({
61
- type: ['multipart/mixed', 'multipart/related', 'text/*', 'application/xml', 'application/octet-stream'],
61
+ });
62
+ // For file contents and upload sessions, always parse as raw regardless of Content-Type.
63
+ // This prevents express.json() from attempting to parse invalid JSON and crashing.
64
+ app.use((req, res, next) => {
65
+ if (req.path.endsWith('/content') || req.path.includes('/upload-sessions')) {
66
+ rawParser(req, res, next);
67
+ }
68
+ else {
69
+ next();
70
+ }
71
+ });
72
+ app.use(express_1.default.json({
62
73
  verify: (req, res, buf) => {
63
74
  req.rawBody = buf;
64
75
  }
65
76
  }));
66
- // Explicit raw body for binary uploads
67
- app.use(express_1.default.raw({
68
- type: '*/*',
69
- limit: '50mb',
77
+ app.use(express_1.default.text({
78
+ type: ['multipart/mixed', 'multipart/related', 'text/*', 'application/xml', 'application/octet-stream'],
70
79
  verify: (req, res, buf) => {
71
80
  req.rawBody = buf;
72
81
  }
73
82
  }));
83
+ // Explicit raw body for binary uploads (catch-all for other routes if not json/text)
84
+ app.use(rawParser);
74
85
  // Batch Route
75
86
  app.post('/v1.0/$batch', batch_1.handleBatchRequest);
76
87
  // Debug
@@ -101,6 +112,20 @@ const createApp = (config = {}) => {
101
112
  next();
102
113
  });
103
114
  app.use((0, v1_1.createV1Router)());
115
+ // Error handler for body-parser syntax errors
116
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
117
+ app.use((err, req, res, next) => {
118
+ if (err instanceof SyntaxError && 'status' in err && err.status === 400 && 'body' in err) {
119
+ res.status(400).json({
120
+ error: {
121
+ code: 'invalidRequest',
122
+ message: err.message
123
+ }
124
+ });
125
+ return;
126
+ }
127
+ next(err);
128
+ });
104
129
  return app;
105
130
  };
106
131
  exports.createApp = createApp;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "microsoft-onedrive-mock",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Mock-Server that simulates being Microsoft OneDrive. Used for testing.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",