@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.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { reducer } from './chunk-KT7MO6Z4.js';
2
- import { targets, getHeaderName } from './chunk-R65NNSSK.js';
3
- export { addClientPlugin, addTarget, addTargetClient } from './chunk-R65NNSSK.js';
2
+ import { targets, getHeaderName } from './chunk-ZPGGGDJK.js';
3
+ export { addClientPlugin, addTarget, addTargetClient } from './chunk-ZPGGGDJK.js';
4
4
  import './chunk-Y7NI4MMY.js';
5
5
  import { parse, format } from 'url';
6
6
  import { stringify } from 'qs';
@@ -3273,22 +3273,22 @@ var literalRepresentation3 = (value, opts, indentLevel) => {
3273
3273
  return value.toString();
3274
3274
  default:
3275
3275
  if (value === null || value === void 0) {
3276
- return "";
3276
+ return "nil";
3277
3277
  }
3278
3278
  return `"${value.toString().replace(/"/g, '\\"')}"`;
3279
3279
  }
3280
3280
  };
3281
3281
 
3282
- // src/targets/swift/nsurlsession/client.ts
3283
- var nsurlsession2 = {
3282
+ // src/targets/swift/urlsession/client.ts
3283
+ var urlsession = {
3284
3284
  info: {
3285
- key: "nsurlsession",
3286
- title: "NSURLSession",
3287
- link: "https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html",
3288
- description: "Foundation's NSURLSession request",
3285
+ key: "urlsession",
3286
+ title: "URLSession",
3287
+ link: "https://developer.apple.com/documentation/foundation/urlsession",
3288
+ description: "Foundation's URLSession request",
3289
3289
  extname: ".swift"
3290
3290
  },
3291
- convert: ({ allHeaders, postData, fullUrl, method }, options) => {
3291
+ convert: ({ allHeaders, postData, uriObj, queryObj, method }, options) => {
3292
3292
  const opts = {
3293
3293
  indent: " ",
3294
3294
  pretty: true,
@@ -3301,6 +3301,9 @@ var nsurlsession2 = {
3301
3301
  hasBody: false
3302
3302
  };
3303
3303
  push("import Foundation");
3304
+ push("#if canImport(FoundationNetworking)");
3305
+ push(" import FoundationNetworking");
3306
+ push("#endif");
3304
3307
  if (Object.keys(allHeaders).length) {
3305
3308
  req.hasHeaders = true;
3306
3309
  blank();
@@ -3313,9 +3316,9 @@ var nsurlsession2 = {
3313
3316
  blank();
3314
3317
  if (postData.params?.length) {
3315
3318
  const [head, ...tail] = postData.params;
3316
- push(`let postData = NSMutableData(data: "${head.name}=${head.value}".data(using: String.Encoding.utf8)!)`);
3319
+ push(`${tail.length > 0 ? "var" : "let"} postData = Data("${head.name}=${head.value}".utf8)`);
3317
3320
  tail.forEach(({ name, value }) => {
3318
- push(`postData.append("&${name}=${value}".data(using: String.Encoding.utf8)!)`);
3321
+ push(`postData.append(Data("&${name}=${value}".utf8))`);
3319
3322
  });
3320
3323
  } else {
3321
3324
  req.hasBody = false;
@@ -3325,7 +3328,7 @@ var nsurlsession2 = {
3325
3328
  if (postData.jsonObj) {
3326
3329
  push(`${literalDeclaration("parameters", postData.jsonObj, opts)} as [String : Any]`);
3327
3330
  blank();
3328
- push("let postData = JSONSerialization.data(withJSONObject: parameters, options: [])");
3331
+ push("let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])");
3329
3332
  }
3330
3333
  break;
3331
3334
  case "multipart/form-data":
@@ -3355,37 +3358,47 @@ var nsurlsession2 = {
3355
3358
  break;
3356
3359
  default:
3357
3360
  blank();
3358
- push(`let postData = NSData(data: "${postData.text}".data(using: String.Encoding.utf8)!)`);
3361
+ push(`let postData = Data("${postData.text}".utf8)`);
3359
3362
  }
3360
3363
  }
3361
3364
  blank();
3362
- push(`let request = NSMutableURLRequest(url: NSURL(string: "${fullUrl}")! as URL,`);
3363
- push(" cachePolicy: .useProtocolCachePolicy,");
3364
- push(
3365
- // @ts-expect-error needs better types
3366
- ` timeoutInterval: ${parseInt(opts.timeout, 10).toFixed(1)})`
3367
- );
3365
+ push(`let url = URL(string: "${uriObj.href}")!`);
3366
+ const queries = queryObj ? Object.entries(queryObj) : [];
3367
+ if (queries.length < 1) {
3368
+ push("var request = URLRequest(url: url)");
3369
+ } else {
3370
+ push("var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!");
3371
+ push("let queryItems: [URLQueryItem] = [");
3372
+ queries.forEach((query) => {
3373
+ const key = query[0];
3374
+ const value = query[1];
3375
+ switch (Object.prototype.toString.call(value)) {
3376
+ case "[object String]":
3377
+ push(`${opts.indent}URLQueryItem(name: "${key}", value: "${value}"),`);
3378
+ break;
3379
+ case "[object Array]":
3380
+ value.forEach((val) => {
3381
+ push(`${opts.indent}URLQueryItem(name: "${key}", value: "${val}"),`);
3382
+ });
3383
+ break;
3384
+ }
3385
+ });
3386
+ push("]");
3387
+ push("components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems");
3388
+ blank();
3389
+ push("var request = URLRequest(url: components.url!)");
3390
+ }
3368
3391
  push(`request.httpMethod = "${method}"`);
3369
3392
  if (req.hasHeaders) {
3370
3393
  push("request.allHTTPHeaderFields = headers");
3371
3394
  }
3372
3395
  if (req.hasBody) {
3373
- push("request.httpBody = postData as Data");
3396
+ push("request.httpBody = postData");
3374
3397
  }
3375
3398
  blank();
3376
- push("let session = URLSession.shared");
3377
- push(
3378
- "let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in"
3379
- );
3380
- push("if (error != nil) {", 1);
3381
- push("print(error as Any)", 2);
3382
- push("} else {", 1);
3383
- push("let httpResponse = response as? HTTPURLResponse", 2);
3384
- push("print(httpResponse)", 2);
3385
- push("}", 1);
3386
- push("})");
3399
+ push("let (data, response) = try await URLSession.shared.data(for: request)");
3400
+ push("print(String(decoding: data, as: UTF8.self))");
3387
3401
  blank();
3388
- push("dataTask.resume()");
3389
3402
  return join();
3390
3403
  }
3391
3404
  };
@@ -3395,10 +3408,10 @@ var swift = {
3395
3408
  info: {
3396
3409
  key: "swift",
3397
3410
  title: "Swift",
3398
- default: "nsurlsession"
3411
+ default: "urlsession"
3399
3412
  },
3400
3413
  clientsById: {
3401
- nsurlsession: nsurlsession2
3414
+ urlsession
3402
3415
  }
3403
3416
  };
3404
3417