@scalar/snippetz 0.7.5 → 0.7.6

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.
@@ -160,12 +160,29 @@ function addBodyContent(lines, request) {
160
160
  }
161
161
  else if (mimeType === 'multipart/form-data' && params) {
162
162
  lines.push('var content = new MultipartFormDataContent();');
163
+ let multipartContentIndex = 0;
163
164
  for (const param of params) {
164
165
  if (param.fileName !== undefined) {
165
- lines.push(`content.Add(new StreamContent(File.OpenRead("${param.fileName}")), "${param.name}", "${param.fileName}");`);
166
+ if (param.contentType) {
167
+ const contentName = `fileContent${multipartContentIndex++}`;
168
+ lines.push(`var ${contentName} = new StreamContent(File.OpenRead("${escapeCSharpString(param.fileName)}"));`);
169
+ lines.push(`${contentName}.Headers.ContentType = new MediaTypeHeaderValue("${param.contentType}");`);
170
+ lines.push(`content.Add(${contentName}, "${escapeCSharpString(param.name)}", "${escapeCSharpString(param.fileName)}");`);
171
+ }
172
+ else {
173
+ lines.push(`content.Add(new StreamContent(File.OpenRead("${escapeCSharpString(param.fileName)}")), "${escapeCSharpString(param.name)}", "${escapeCSharpString(param.fileName)}");`);
174
+ }
166
175
  }
167
176
  else {
168
- lines.push(`content.Add(new StringContent("${param.value}"), "${param.name}");`);
177
+ if (param.contentType) {
178
+ const contentName = `stringContent${multipartContentIndex++}`;
179
+ lines.push(`var ${contentName} = new StringContent("${escapeCSharpString(param.value ?? '')}");`);
180
+ lines.push(`${contentName}.Headers.ContentType = new MediaTypeHeaderValue("${param.contentType}");`);
181
+ lines.push(`content.Add(${contentName}, "${escapeCSharpString(param.name)}");`);
182
+ }
183
+ else {
184
+ lines.push(`content.Add(new StringContent("${escapeCSharpString(param.value ?? '')}"), "${escapeCSharpString(param.name)}");`);
185
+ }
169
186
  }
170
187
  }
171
188
  lines.push('request.Content = content;');
@@ -195,6 +212,9 @@ function createRawStringLiteral(text) {
195
212
  const quotes = '"'.repeat(quoteCount);
196
213
  return `${quotes}\n${text}\n${quotes}`;
197
214
  }
215
+ function escapeCSharpString(text) {
216
+ return text.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
217
+ }
198
218
  /**
199
219
  * Check if a value looks like a media type
200
220
  */
@@ -127,16 +127,29 @@ function generateGenericContentCode(postData, contentType) {
127
127
  function generateMultipartFormDataCode(postData) {
128
128
  let code = 'let content = new MultipartFormDataContent()\n';
129
129
  let fileIndex = 0;
130
+ let stringContentIndex = 0;
130
131
  for (const param of postData.params) {
131
132
  if (param.value === 'BINARY') {
132
133
  const escapedFileName = escapeString(param.fileName ?? '');
133
134
  code += `let fileStreamContent_${fileIndex} = new StreamContent(File.OpenRead("${escapedFileName}"))\n`;
134
- code += `fileStreamContent_${fileIndex}.Headers.ContentType <- MediaTypeHeaderValue("${escapeString(param.contentType ?? '')}")\n`;
135
+ if (param.contentType) {
136
+ code += `fileStreamContent_${fileIndex}.Headers.ContentType <- MediaTypeHeaderValue("${escapeString(param.contentType)}")\n`;
137
+ }
135
138
  code += `content.Add(fileStreamContent_${fileIndex}, "${escapedFileName}", "${escapedFileName}")\n`;
136
139
  fileIndex++;
137
140
  }
138
141
  else {
139
- code += `content.Add(new StringContent("${escapeString(param.value ?? '')}"), "${escapeString(param.name ?? '')}")\n`;
142
+ const escapedName = escapeString(param.name ?? '');
143
+ const escapedValue = escapeString(param.value ?? '');
144
+ if (param.contentType) {
145
+ code += `let stringContent_${stringContentIndex} = new StringContent("${escapedValue}")\n`;
146
+ code += `stringContent_${stringContentIndex}.Headers.ContentType <- MediaTypeHeaderValue("${escapeString(param.contentType)}")\n`;
147
+ code += `content.Add(stringContent_${stringContentIndex}, "${escapedName}")\n`;
148
+ stringContentIndex++;
149
+ }
150
+ else {
151
+ code += `content.Add(new StringContent("${escapedValue}"), "${escapedName}")\n`;
152
+ }
140
153
  }
141
154
  }
142
155
  return code;
@@ -1 +1 @@
1
- {"version":3,"file":"http11.d.ts","sourceRoot":"","sources":["../../../../src/plugins/http/http11/http11.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAEpD;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,MAoHxB,CAAA"}
1
+ {"version":3,"file":"http11.d.ts","sourceRoot":"","sources":["../../../../src/plugins/http/http11/http11.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAEpD;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,MAsHxB,CAAA"}
@@ -85,10 +85,11 @@ export const httpHttp11 = {
85
85
  body =
86
86
  normalizedRequest.postData.params
87
87
  .map((param) => {
88
+ const contentTypeHeader = param.contentType ? `Content-Type: ${param.contentType}\r\n` : '';
88
89
  if (param.fileName) {
89
- return `--${boundary}\r\nContent-Disposition: form-data; name="${param.name}"; filename="${param.fileName}"\r\n\r\n`;
90
+ return `--${boundary}\r\nContent-Disposition: form-data; name="${param.name}"; filename="${param.fileName}"\r\n${contentTypeHeader}\r\n`;
90
91
  }
91
- return `--${boundary}\r\nContent-Disposition: form-data; name="${param.name}"\r\n\r\n${param.value}\r\n`;
92
+ return `--${boundary}\r\nContent-Disposition: form-data; name="${param.name}"\r\n${contentTypeHeader}\r\n${param.value ?? ''}\r\n`;
92
93
  })
93
94
  .join('') + `--${boundary}--\r\n`;
94
95
  }
@@ -1 +1 @@
1
- {"version":3,"file":"guzzle.d.ts","sourceRoot":"","sources":["../../../../src/plugins/php/guzzle/guzzle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MAyGvB,CAAA"}
1
+ {"version":3,"file":"guzzle.d.ts","sourceRoot":"","sources":["../../../../src/plugins/php/guzzle/guzzle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MAiHvB,CAAA"}
@@ -59,10 +59,16 @@ export const phpGuzzle = {
59
59
  }
60
60
  else if (request.postData.mimeType === 'multipart/form-data') {
61
61
  if (request.postData.params) {
62
- options.multipart = request.postData.params.map((param) => ({
63
- name: param.name,
64
- contents: param.fileName ? new Raw(`fopen('${param.fileName}', 'r')`) : param.value || '',
65
- }));
62
+ options.multipart = request.postData.params.map((param) => {
63
+ const part = {
64
+ name: param.name,
65
+ contents: param.fileName ? new Raw(`fopen('${param.fileName}', 'r')`) : param.value || '',
66
+ };
67
+ if (param.contentType) {
68
+ part.headers = { 'Content-Type': param.contentType };
69
+ }
70
+ return part;
71
+ });
66
72
  }
67
73
  else if (request.postData.text) {
68
74
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"requestsLike.d.ts","sourceRoot":"","sources":["../../../src/plugins/python/requestsLike.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AA0B7E,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,EAC7B,aAAa,CAAC,EAAE,mBAAmB,UAsIpC"}
1
+ {"version":3,"file":"requestsLike.d.ts","sourceRoot":"","sources":["../../../src/plugins/python/requestsLike.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AA0B7E,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,EAC7B,aAAa,CAAC,EAAE,mBAAmB,UAuJpC"}
@@ -67,13 +67,30 @@ export function requestsLikeGenerate(clientVar, request, configuration) {
67
67
  const formData = {};
68
68
  params.forEach((param) => {
69
69
  if (param.fileName !== undefined) {
70
- files.push({ key: param.name, file: `open("${param.fileName}", "rb")` });
70
+ const name = JSON.stringify(param.name);
71
+ const fileName = JSON.stringify(param.fileName);
72
+ const file = `open(${fileName}, "rb")`;
73
+ if (param.contentType) {
74
+ const contentType = JSON.stringify(param.contentType);
75
+ files.push(`(${name}, (${fileName}, ${file}, ${contentType}))`);
76
+ }
77
+ else {
78
+ files.push(`(${name}, ${file})`);
79
+ }
71
80
  }
72
81
  else if (param.value !== undefined) {
73
- formData[param.name] = param.value;
82
+ if (param.contentType) {
83
+ const name = JSON.stringify(param.name);
84
+ const value = JSON.stringify(param.value);
85
+ const contentType = JSON.stringify(param.contentType);
86
+ files.push(`(${name}, (None, ${value}, ${contentType}))`);
87
+ }
88
+ else {
89
+ formData[param.name] = param.value;
90
+ }
74
91
  }
75
92
  });
76
- if (Object.keys(files).length) {
93
+ if (files.length) {
77
94
  options.files = files;
78
95
  }
79
96
  if (Object.keys(formData).length) {
@@ -101,7 +118,7 @@ export function requestsLikeGenerate(clientVar, request, configuration) {
101
118
  formattedParams.push(`${key}=(${convertToPythonSyntax(JSON.stringify(value[0]))}, ${convertToPythonSyntax(JSON.stringify(value[1]))})`);
102
119
  }
103
120
  else if (key === 'files') {
104
- const filesTuples = value.map(({ key, file }) => ` ("${key}", ${file})`);
121
+ const filesTuples = value.map((tuple) => ` ${tuple}`);
105
122
  const filesStr = '[\n' + filesTuples.join(',\n') + '\n ]';
106
123
  formattedParams.push(`${key}=${filesStr}`);
107
124
  }
@@ -41,9 +41,25 @@ export const rustReqwest = {
41
41
  */
42
42
  const createMultipartPart = (param) => {
43
43
  if (param.fileName) {
44
+ const part = [
45
+ indent(2, `let part = reqwest::multipart::Part::text(${wrapInDoubleQuotes(param.value || '')})`),
46
+ indent(3, `.file_name(${wrapInDoubleQuotes(param.fileName)})`),
47
+ ];
48
+ if (param.contentType) {
49
+ part.push(indent(3, `.mime_str(${wrapInDoubleQuotes(param.contentType)})`));
50
+ part.push(indent(3, '.unwrap();'));
51
+ }
52
+ else {
53
+ part[part.length - 1] += ';';
54
+ }
55
+ part.push(indent(2, `form = form.part(${wrapInDoubleQuotes(param.name)}, part);`));
56
+ return part.join('\n');
57
+ }
58
+ if (param.contentType) {
44
59
  return [
45
60
  indent(2, `let part = reqwest::multipart::Part::text(${wrapInDoubleQuotes(param.value || '')})`),
46
- indent(3, `.file_name(${wrapInDoubleQuotes(param.fileName)});`),
61
+ indent(3, `.mime_str(${wrapInDoubleQuotes(param.contentType)})`),
62
+ indent(3, '.unwrap();'),
47
63
  indent(2, `form = form.part(${wrapInDoubleQuotes(param.name)}, part);`),
48
64
  ].join('\n');
49
65
  }
@@ -1 +1 @@
1
- {"version":3,"file":"curl.d.ts","sourceRoot":"","sources":["../../../../src/plugins/shell/curl/curl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAIpD;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MAmIvB,CAAA"}
1
+ {"version":3,"file":"curl.d.ts","sourceRoot":"","sources":["../../../../src/plugins/shell/curl/curl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAIpD;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MAoIvB,CAAA"}
@@ -102,12 +102,13 @@ export const shellCurl = {
102
102
  // Handle multipart form data
103
103
  normalizedRequest.postData.params.forEach((param) => {
104
104
  const escapedName = escapeSingleQuotes(param.name);
105
+ const multipartValueSuffix = param.contentType ? `;type=${param.contentType}` : '';
105
106
  if (param.fileName !== undefined) {
106
- const escapedFileName = escapeSingleQuotes(param.fileName);
107
+ const escapedFileName = escapeSingleQuotes(`${param.fileName}${multipartValueSuffix}`);
107
108
  parts.push(`--form '${escapedName}=@${escapedFileName}'`);
108
109
  }
109
110
  else {
110
- const escapedValue = escapeSingleQuotes(param.value ?? '');
111
+ const escapedValue = escapeSingleQuotes(`${param.value ?? ''}${multipartValueSuffix}`);
111
112
  parts.push(`--form '${escapedName}=${escapedValue}'`);
112
113
  }
113
114
  });
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "url": "git+https://github.com/scalar/scalar.git",
10
10
  "directory": "packages/snippetz"
11
11
  },
12
- "version": "0.7.5",
12
+ "version": "0.7.6",
13
13
  "engines": {
14
14
  "node": ">=22"
15
15
  },