@readme/httpsnippet 10.0.0 → 10.0.1

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-M6LAZDIU.js';
3
+ export { addClientPlugin, addTarget, addTargetClient } from './chunk-M6LAZDIU.js';
4
4
  import './chunk-Y7NI4MMY.js';
5
5
  import { parse, format } from 'url';
6
6
  import { stringify } from 'qs';
@@ -3273,19 +3273,19 @@ 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
3291
  convert: ({ allHeaders, postData, fullUrl, method }, options) => {
@@ -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,22 @@ 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(`var request = URLRequest(url: URL(string: "${fullUrl}")!)`);
3368
3366
  push(`request.httpMethod = "${method}"`);
3369
3367
  if (req.hasHeaders) {
3370
3368
  push("request.allHTTPHeaderFields = headers");
3371
3369
  }
3372
3370
  if (req.hasBody) {
3373
- push("request.httpBody = postData as Data");
3371
+ push("request.httpBody = postData");
3374
3372
  }
3375
3373
  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("})");
3374
+ push("let (data, response) = try await URLSession.shared.data(with: request)");
3375
+ push("print(String(decoding: data, as: UTF8.self))");
3387
3376
  blank();
3388
- push("dataTask.resume()");
3389
3377
  return join();
3390
3378
  }
3391
3379
  };
@@ -3395,10 +3383,10 @@ var swift = {
3395
3383
  info: {
3396
3384
  key: "swift",
3397
3385
  title: "Swift",
3398
- default: "nsurlsession"
3386
+ default: "urlsession"
3399
3387
  },
3400
3388
  clientsById: {
3401
- nsurlsession: nsurlsession2
3389
+ urlsession
3402
3390
  }
3403
3391
  };
3404
3392