@readme/httpsnippet 10.0.0 → 10.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.
@@ -3208,22 +3208,22 @@ var literalRepresentation3 = (value, opts, indentLevel) => {
3208
3208
  return value.toString();
3209
3209
  default:
3210
3210
  if (value === null || value === void 0) {
3211
- return "";
3211
+ return "nil";
3212
3212
  }
3213
3213
  return `"${value.toString().replace(/"/g, '\\"')}"`;
3214
3214
  }
3215
3215
  };
3216
3216
 
3217
- // src/targets/swift/nsurlsession/client.ts
3218
- var nsurlsession2 = {
3217
+ // src/targets/swift/urlsession/client.ts
3218
+ var urlsession = {
3219
3219
  info: {
3220
- key: "nsurlsession",
3221
- title: "NSURLSession",
3222
- link: "https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html",
3223
- description: "Foundation's NSURLSession request",
3220
+ key: "urlsession",
3221
+ title: "URLSession",
3222
+ link: "https://developer.apple.com/documentation/foundation/urlsession",
3223
+ description: "Foundation's URLSession request",
3224
3224
  extname: ".swift"
3225
3225
  },
3226
- convert: ({ allHeaders, postData, fullUrl, method }, options) => {
3226
+ convert: ({ allHeaders, postData, uriObj, queryObj, method }, options) => {
3227
3227
  const opts = {
3228
3228
  indent: " ",
3229
3229
  pretty: true,
@@ -3236,6 +3236,9 @@ var nsurlsession2 = {
3236
3236
  hasBody: false
3237
3237
  };
3238
3238
  push("import Foundation");
3239
+ push("#if canImport(FoundationNetworking)");
3240
+ push(" import FoundationNetworking");
3241
+ push("#endif");
3239
3242
  if (Object.keys(allHeaders).length) {
3240
3243
  req.hasHeaders = true;
3241
3244
  blank();
@@ -3248,9 +3251,9 @@ var nsurlsession2 = {
3248
3251
  blank();
3249
3252
  if (postData.params?.length) {
3250
3253
  const [head, ...tail] = postData.params;
3251
- push(`let postData = NSMutableData(data: "${head.name}=${head.value}".data(using: String.Encoding.utf8)!)`);
3254
+ push(`${tail.length > 0 ? "var" : "let"} postData = Data("${head.name}=${head.value}".utf8)`);
3252
3255
  tail.forEach(({ name, value }) => {
3253
- push(`postData.append("&${name}=${value}".data(using: String.Encoding.utf8)!)`);
3256
+ push(`postData.append(Data("&${name}=${value}".utf8))`);
3254
3257
  });
3255
3258
  } else {
3256
3259
  req.hasBody = false;
@@ -3260,7 +3263,7 @@ var nsurlsession2 = {
3260
3263
  if (postData.jsonObj) {
3261
3264
  push(`${literalDeclaration("parameters", postData.jsonObj, opts)} as [String : Any]`);
3262
3265
  blank();
3263
- push("let postData = JSONSerialization.data(withJSONObject: parameters, options: [])");
3266
+ push("let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])");
3264
3267
  }
3265
3268
  break;
3266
3269
  case "multipart/form-data":
@@ -3290,37 +3293,47 @@ var nsurlsession2 = {
3290
3293
  break;
3291
3294
  default:
3292
3295
  blank();
3293
- push(`let postData = NSData(data: "${postData.text}".data(using: String.Encoding.utf8)!)`);
3296
+ push(`let postData = Data("${postData.text}".utf8)`);
3294
3297
  }
3295
3298
  }
3296
3299
  blank();
3297
- push(`let request = NSMutableURLRequest(url: NSURL(string: "${fullUrl}")! as URL,`);
3298
- push(" cachePolicy: .useProtocolCachePolicy,");
3299
- push(
3300
- // @ts-expect-error needs better types
3301
- ` timeoutInterval: ${parseInt(opts.timeout, 10).toFixed(1)})`
3302
- );
3300
+ push(`let url = URL(string: "${uriObj.href}")!`);
3301
+ const queries = queryObj ? Object.entries(queryObj) : [];
3302
+ if (queries.length < 1) {
3303
+ push("var request = URLRequest(url: url)");
3304
+ } else {
3305
+ push("var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!");
3306
+ push("let queryItems: [URLQueryItem] = [");
3307
+ queries.forEach((query) => {
3308
+ const key = query[0];
3309
+ const value = query[1];
3310
+ switch (Object.prototype.toString.call(value)) {
3311
+ case "[object String]":
3312
+ push(`${opts.indent}URLQueryItem(name: "${key}", value: "${value}"),`);
3313
+ break;
3314
+ case "[object Array]":
3315
+ value.forEach((val) => {
3316
+ push(`${opts.indent}URLQueryItem(name: "${key}", value: "${val}"),`);
3317
+ });
3318
+ break;
3319
+ }
3320
+ });
3321
+ push("]");
3322
+ push("components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems");
3323
+ blank();
3324
+ push("var request = URLRequest(url: components.url!)");
3325
+ }
3303
3326
  push(`request.httpMethod = "${method}"`);
3304
3327
  if (req.hasHeaders) {
3305
3328
  push("request.allHTTPHeaderFields = headers");
3306
3329
  }
3307
3330
  if (req.hasBody) {
3308
- push("request.httpBody = postData as Data");
3331
+ push("request.httpBody = postData");
3309
3332
  }
3310
3333
  blank();
3311
- push("let session = URLSession.shared");
3312
- push(
3313
- "let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in"
3314
- );
3315
- push("if (error != nil) {", 1);
3316
- push("print(error as Any)", 2);
3317
- push("} else {", 1);
3318
- push("let httpResponse = response as? HTTPURLResponse", 2);
3319
- push("print(httpResponse)", 2);
3320
- push("}", 1);
3321
- push("})");
3334
+ push("let (data, response) = try await URLSession.shared.data(for: request)");
3335
+ push("print(String(decoding: data, as: UTF8.self))");
3322
3336
  blank();
3323
- push("dataTask.resume()");
3324
3337
  return join();
3325
3338
  }
3326
3339
  };
@@ -3330,10 +3343,10 @@ var swift = {
3330
3343
  info: {
3331
3344
  key: "swift",
3332
3345
  title: "Swift",
3333
- default: "nsurlsession"
3346
+ default: "urlsession"
3334
3347
  },
3335
3348
  clientsById: {
3336
- nsurlsession: nsurlsession2
3349
+ urlsession
3337
3350
  }
3338
3351
  };
3339
3352
 
@@ -3452,4 +3465,4 @@ var addTargetClient = (targetId, client) => {
3452
3465
 
3453
3466
  export { addClientPlugin, addTarget, addTargetClient, getHeaderName, isClient, isTarget, targets };
3454
3467
  //# sourceMappingURL=out.js.map
3455
- //# sourceMappingURL=chunk-R65NNSSK.js.map
3468
+ //# sourceMappingURL=chunk-ZPGGGDJK.js.map