@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.
package/dist/index.cjs CHANGED
@@ -3292,22 +3292,22 @@ var literalRepresentation3 = (value, opts, indentLevel) => {
3292
3292
  return value.toString();
3293
3293
  default:
3294
3294
  if (value === null || value === void 0) {
3295
- return "";
3295
+ return "nil";
3296
3296
  }
3297
3297
  return `"${value.toString().replace(/"/g, '\\"')}"`;
3298
3298
  }
3299
3299
  };
3300
3300
 
3301
- // src/targets/swift/nsurlsession/client.ts
3302
- var nsurlsession2 = {
3301
+ // src/targets/swift/urlsession/client.ts
3302
+ var urlsession = {
3303
3303
  info: {
3304
- key: "nsurlsession",
3305
- title: "NSURLSession",
3306
- link: "https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html",
3307
- description: "Foundation's NSURLSession request",
3304
+ key: "urlsession",
3305
+ title: "URLSession",
3306
+ link: "https://developer.apple.com/documentation/foundation/urlsession",
3307
+ description: "Foundation's URLSession request",
3308
3308
  extname: ".swift"
3309
3309
  },
3310
- convert: ({ allHeaders, postData, fullUrl, method }, options) => {
3310
+ convert: ({ allHeaders, postData, uriObj, queryObj, method }, options) => {
3311
3311
  const opts = {
3312
3312
  indent: " ",
3313
3313
  pretty: true,
@@ -3320,6 +3320,9 @@ var nsurlsession2 = {
3320
3320
  hasBody: false
3321
3321
  };
3322
3322
  push("import Foundation");
3323
+ push("#if canImport(FoundationNetworking)");
3324
+ push(" import FoundationNetworking");
3325
+ push("#endif");
3323
3326
  if (Object.keys(allHeaders).length) {
3324
3327
  req.hasHeaders = true;
3325
3328
  blank();
@@ -3332,9 +3335,9 @@ var nsurlsession2 = {
3332
3335
  blank();
3333
3336
  if (postData.params?.length) {
3334
3337
  const [head, ...tail] = postData.params;
3335
- push(`let postData = NSMutableData(data: "${head.name}=${head.value}".data(using: String.Encoding.utf8)!)`);
3338
+ push(`${tail.length > 0 ? "var" : "let"} postData = Data("${head.name}=${head.value}".utf8)`);
3336
3339
  tail.forEach(({ name, value }) => {
3337
- push(`postData.append("&${name}=${value}".data(using: String.Encoding.utf8)!)`);
3340
+ push(`postData.append(Data("&${name}=${value}".utf8))`);
3338
3341
  });
3339
3342
  } else {
3340
3343
  req.hasBody = false;
@@ -3344,7 +3347,7 @@ var nsurlsession2 = {
3344
3347
  if (postData.jsonObj) {
3345
3348
  push(`${literalDeclaration("parameters", postData.jsonObj, opts)} as [String : Any]`);
3346
3349
  blank();
3347
- push("let postData = JSONSerialization.data(withJSONObject: parameters, options: [])");
3350
+ push("let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])");
3348
3351
  }
3349
3352
  break;
3350
3353
  case "multipart/form-data":
@@ -3374,37 +3377,47 @@ var nsurlsession2 = {
3374
3377
  break;
3375
3378
  default:
3376
3379
  blank();
3377
- push(`let postData = NSData(data: "${postData.text}".data(using: String.Encoding.utf8)!)`);
3380
+ push(`let postData = Data("${postData.text}".utf8)`);
3378
3381
  }
3379
3382
  }
3380
3383
  blank();
3381
- push(`let request = NSMutableURLRequest(url: NSURL(string: "${fullUrl}")! as URL,`);
3382
- push(" cachePolicy: .useProtocolCachePolicy,");
3383
- push(
3384
- // @ts-expect-error needs better types
3385
- ` timeoutInterval: ${parseInt(opts.timeout, 10).toFixed(1)})`
3386
- );
3384
+ push(`let url = URL(string: "${uriObj.href}")!`);
3385
+ const queries = queryObj ? Object.entries(queryObj) : [];
3386
+ if (queries.length < 1) {
3387
+ push("var request = URLRequest(url: url)");
3388
+ } else {
3389
+ push("var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!");
3390
+ push("let queryItems: [URLQueryItem] = [");
3391
+ queries.forEach((query) => {
3392
+ const key = query[0];
3393
+ const value = query[1];
3394
+ switch (Object.prototype.toString.call(value)) {
3395
+ case "[object String]":
3396
+ push(`${opts.indent}URLQueryItem(name: "${key}", value: "${value}"),`);
3397
+ break;
3398
+ case "[object Array]":
3399
+ value.forEach((val) => {
3400
+ push(`${opts.indent}URLQueryItem(name: "${key}", value: "${val}"),`);
3401
+ });
3402
+ break;
3403
+ }
3404
+ });
3405
+ push("]");
3406
+ push("components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems");
3407
+ blank();
3408
+ push("var request = URLRequest(url: components.url!)");
3409
+ }
3387
3410
  push(`request.httpMethod = "${method}"`);
3388
3411
  if (req.hasHeaders) {
3389
3412
  push("request.allHTTPHeaderFields = headers");
3390
3413
  }
3391
3414
  if (req.hasBody) {
3392
- push("request.httpBody = postData as Data");
3415
+ push("request.httpBody = postData");
3393
3416
  }
3394
3417
  blank();
3395
- push("let session = URLSession.shared");
3396
- push(
3397
- "let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in"
3398
- );
3399
- push("if (error != nil) {", 1);
3400
- push("print(error as Any)", 2);
3401
- push("} else {", 1);
3402
- push("let httpResponse = response as? HTTPURLResponse", 2);
3403
- push("print(httpResponse)", 2);
3404
- push("}", 1);
3405
- push("})");
3418
+ push("let (data, response) = try await URLSession.shared.data(for: request)");
3419
+ push("print(String(decoding: data, as: UTF8.self))");
3406
3420
  blank();
3407
- push("dataTask.resume()");
3408
3421
  return join();
3409
3422
  }
3410
3423
  };
@@ -3414,10 +3427,10 @@ var swift = {
3414
3427
  info: {
3415
3428
  key: "swift",
3416
3429
  title: "Swift",
3417
- default: "nsurlsession"
3430
+ default: "urlsession"
3418
3431
  },
3419
3432
  clientsById: {
3420
- nsurlsession: nsurlsession2
3433
+ urlsession
3421
3434
  }
3422
3435
  };
3423
3436