@qlover/fe-corekit 3.1.0 → 3.1.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.cjs CHANGED
@@ -3981,6 +3981,7 @@ var SimpleUrlBuilder = class {
3981
3981
  };
3982
3982
 
3983
3983
  // src/request/impl/RequestPlugin.ts
3984
+ var import_lodash_es4 = require("lodash-es");
3984
3985
  var RequestPlugin = class {
3985
3986
  pluginName = "RequestPlugin";
3986
3987
  config;
@@ -4035,13 +4036,23 @@ var RequestPlugin = class {
4035
4036
  * @override
4036
4037
  */
4037
4038
  onBefore(ctx) {
4038
- const mergedConfig = this.mergeConfig(ctx.parameters);
4039
+ ctx.setParameters(this.mergeConfig(ctx.parameters));
4040
+ }
4041
+ /**
4042
+ * Main request handler
4043
+ *
4044
+ * This is the core of the plugin. It merges default plugin configuration with request context configuration,
4045
+ * processes request data, builds the URL, and injects headers.
4046
+ *
4047
+ * @param config - Request configuration
4048
+ * @returns Merged configuration with processed data, built URL, and injected headers
4049
+ */
4050
+ mergeConfig(config) {
4051
+ const mergedConfig = this.createConfig(config);
4039
4052
  const processedData = this.processRequestData(mergedConfig);
4040
4053
  const builtUrl = this.buildUrl(mergedConfig);
4041
4054
  const injectedHeaders = this.injectHeaders(mergedConfig);
4042
- ctx.setParameters({
4043
- ...ctx.parameters,
4044
- ...mergedConfig,
4055
+ return Object.assign(mergedConfig, {
4045
4056
  data: processedData,
4046
4057
  url: builtUrl,
4047
4058
  headers: injectedHeaders
@@ -4059,8 +4070,8 @@ var RequestPlugin = class {
4059
4070
  * @param contextConfig - Configuration from request context
4060
4071
  * @returns Merged configuration
4061
4072
  */
4062
- mergeConfig(contextConfig) {
4063
- const merged = { ...this.config, ...contextConfig };
4073
+ createConfig(contextConfig) {
4074
+ const merged = Object.assign((0, import_lodash_es4.clone)(this.config), contextConfig);
4064
4075
  if (!("data" in contextConfig) && "data" in this.config) {
4065
4076
  merged.data = this.config.data;
4066
4077
  }
@@ -4074,13 +4085,10 @@ var RequestPlugin = class {
4074
4085
  * @throws {Error} If the built URL is empty or invalid
4075
4086
  */
4076
4087
  buildUrl(config) {
4077
- const url = this.urlBuilder.buildUrl(config);
4078
- if (!url || url.trim() === "") {
4079
- throw new Error(
4080
- `RequestPlugin: Invalid URL. URL cannot be empty. baseURL: ${config.baseURL ?? "undefined"}, url: ${config.url ?? "undefined"}`
4081
- );
4088
+ if (config.allowEmptyUrl === false && !config.url && !config.baseURL) {
4089
+ throw new Error("Empty URL is not allowed");
4082
4090
  }
4083
- return url;
4091
+ return this.urlBuilder.buildUrl(config);
4084
4092
  }
4085
4093
  /**
4086
4094
  * Inject default headers into request configuration
@@ -4134,7 +4142,7 @@ var RequestPlugin = class {
4134
4142
  };
4135
4143
 
4136
4144
  // src/request/impl/ResponsePlugin.ts
4137
- var import_lodash_es4 = require("lodash-es");
4145
+ var import_lodash_es5 = require("lodash-es");
4138
4146
 
4139
4147
  // src/request/utils/isRequestAdapterResponse.ts
4140
4148
  function isRequestAdapterResponse(value) {
@@ -4215,18 +4223,17 @@ var ResponsePlugin = class {
4215
4223
  async onSuccess(context) {
4216
4224
  const returnValue = context.returnValue;
4217
4225
  const config = context.parameters;
4226
+ const result = await this.handleResponse(returnValue, config);
4227
+ if (result) {
4228
+ context.setReturnValue(result);
4229
+ }
4230
+ }
4231
+ handleResponse(returnValue, config) {
4218
4232
  if (returnValue instanceof Response) {
4219
- const processedResponse = await this.processResponse(returnValue, config);
4220
- context.setReturnValue(processedResponse);
4221
- return;
4233
+ return this.processResponse(returnValue, config);
4222
4234
  }
4223
4235
  if (isRequestAdapterResponse(returnValue)) {
4224
- const processedResponse = await this.processAdapterResponse(
4225
- returnValue,
4226
- config
4227
- );
4228
- context.setReturnValue(processedResponse);
4229
- return;
4236
+ return this.processAdapterResponse(returnValue, config);
4230
4237
  }
4231
4238
  }
4232
4239
  /**
@@ -4298,7 +4305,7 @@ var ResponsePlugin = class {
4298
4305
  * ```
4299
4306
  */
4300
4307
  async parseResponseData(response, responseType) {
4301
- if ((0, import_lodash_es4.isFunction)(this.config.responseDataParser)) {
4308
+ if ((0, import_lodash_es5.isFunction)(this.config.responseDataParser)) {
4302
4309
  return await this.config.responseDataParser(response, responseType);
4303
4310
  }
4304
4311
  return await this.defaultParseResponseData(response, responseType);
@@ -4322,7 +4329,7 @@ var ResponsePlugin = class {
4322
4329
  if (parser === false) {
4323
4330
  return response;
4324
4331
  }
4325
- if ((0, import_lodash_es4.isFunction)(parser)) {
4332
+ if ((0, import_lodash_es5.isFunction)(parser)) {
4326
4333
  return await parser(response);
4327
4334
  }
4328
4335
  return await this.fallbackParseByContentType(response, normalizedType);
@@ -4340,13 +4347,13 @@ var ResponsePlugin = class {
4340
4347
  const lowerContentType = contentType.toLowerCase();
4341
4348
  if (lowerContentType.includes("application/json")) {
4342
4349
  const jsonParser = this.parsers.json;
4343
- if ((0, import_lodash_es4.isFunction)(jsonParser)) {
4350
+ if ((0, import_lodash_es5.isFunction)(jsonParser)) {
4344
4351
  return jsonParser;
4345
4352
  }
4346
4353
  }
4347
4354
  if (lowerContentType.includes("text/") || lowerContentType.includes("application/xml") || lowerContentType.includes("application/xhtml")) {
4348
4355
  const textParser = this.parsers.text;
4349
- if ((0, import_lodash_es4.isFunction)(textParser)) {
4356
+ if ((0, import_lodash_es5.isFunction)(textParser)) {
4350
4357
  return textParser;
4351
4358
  }
4352
4359
  }
@@ -4363,11 +4370,11 @@ var ResponsePlugin = class {
4363
4370
  getFallbackParsers() {
4364
4371
  const fallbackParsers = [];
4365
4372
  const jsonParser = this.parsers.json;
4366
- if ((0, import_lodash_es4.isFunction)(jsonParser)) {
4373
+ if ((0, import_lodash_es5.isFunction)(jsonParser)) {
4367
4374
  fallbackParsers.push(jsonParser);
4368
4375
  }
4369
4376
  const textParser = this.parsers.text;
4370
- if ((0, import_lodash_es4.isFunction)(textParser)) {
4377
+ if ((0, import_lodash_es5.isFunction)(textParser)) {
4371
4378
  fallbackParsers.push(textParser);
4372
4379
  }
4373
4380
  return fallbackParsers;
package/dist/index.d.ts CHANGED
@@ -6848,6 +6848,16 @@ type RequestAdapterContext = ExecutorContextInterface<RequestAdapterConfig, unkn
6848
6848
  * ```
6849
6849
  */
6850
6850
  type RequestPluginConfig = HeaderInjectorConfig & {
6851
+ /**
6852
+ * Allow empty URL
6853
+ *
6854
+ * If set to `false`, an error will be thrown when both `url` and `baseURL` are empty.
6855
+ * If set to `true` (default), the URL builder will attempt to build the URL even if they are empty.
6856
+ *
6857
+ * @since 3.1.0
6858
+ * @default `true`
6859
+ */
6860
+ allowEmptyUrl?: boolean;
6851
6861
  requestDataSerializer?: (data: unknown, config: RequestAdapterConfig & Omit<RequestPluginConfig, 'requestDataSerializer'>) => unknown;
6852
6862
  };
6853
6863
  interface RequestPluginInnerConfig {
@@ -6947,6 +6957,16 @@ declare class RequestPlugin implements LifecyclePluginInterface<RequestAdapterCo
6947
6957
  * @override
6948
6958
  */
6949
6959
  onBefore(ctx: RequestAdapterContext): void;
6960
+ /**
6961
+ * Main request handler
6962
+ *
6963
+ * This is the core of the plugin. It merges default plugin configuration with request context configuration,
6964
+ * processes request data, builds the URL, and injects headers.
6965
+ *
6966
+ * @param config - Request configuration
6967
+ * @returns Merged configuration with processed data, built URL, and injected headers
6968
+ */
6969
+ mergeConfig(config: RequestAdapterConfig & RequestPluginConfig): RequestAdapterConfig & RequestPluginConfig;
6950
6970
  /**
6951
6971
  * Merge default plugin configuration with request context configuration
6952
6972
  *
@@ -6959,7 +6979,7 @@ declare class RequestPlugin implements LifecyclePluginInterface<RequestAdapterCo
6959
6979
  * @param contextConfig - Configuration from request context
6960
6980
  * @returns Merged configuration
6961
6981
  */
6962
- protected mergeConfig(contextConfig: RequestAdapterConfig): RequestAdapterConfig & RequestPluginConfig;
6982
+ protected createConfig(contextConfig: RequestAdapterConfig): RequestAdapterConfig & RequestPluginConfig;
6963
6983
  /**
6964
6984
  * Build the URL from the request configuration
6965
6985
  *
@@ -6967,7 +6987,7 @@ declare class RequestPlugin implements LifecyclePluginInterface<RequestAdapterCo
6967
6987
  * @returns The built URL
6968
6988
  * @throws {Error} If the built URL is empty or invalid
6969
6989
  */
6970
- protected buildUrl(config: RequestAdapterConfig): string;
6990
+ protected buildUrl(config: RequestAdapterConfig & RequestPluginConfig): string;
6971
6991
  /**
6972
6992
  * Inject default headers into request configuration
6973
6993
  *
@@ -7263,6 +7283,7 @@ declare class ResponsePlugin implements LifecyclePluginInterface<ResponsePluginC
7263
7283
  * ```
7264
7284
  */
7265
7285
  onSuccess(context: ResponsePluginContext): Promise<void>;
7286
+ handleResponse(returnValue: unknown, config: ResponsePluginConfig & RequestAdapterConfig<unknown>): Promise<RequestAdapterResponse> | undefined;
7266
7287
  /**
7267
7288
  * Validate response status
7268
7289
  *
@@ -6728,15 +6728,29 @@ var qloverFeCorekit = function() {
6728
6728
  * @override
6729
6729
  */ key: "onBefore",
6730
6730
  value: function onBefore(ctx) {
6731
- var mergedConfig = this.mergeConfig(ctx.parameters);
6731
+ ctx.setParameters(this.mergeConfig(ctx.parameters));
6732
+ }
6733
+ },
6734
+ {
6735
+ /**
6736
+ * Main request handler
6737
+ *
6738
+ * This is the core of the plugin. It merges default plugin configuration with request context configuration,
6739
+ * processes request data, builds the URL, and injects headers.
6740
+ *
6741
+ * @param config - Request configuration
6742
+ * @returns Merged configuration with processed data, built URL, and injected headers
6743
+ */ key: "mergeConfig",
6744
+ value: function mergeConfig(config) {
6745
+ var mergedConfig = this.createConfig(config);
6732
6746
  var processedData = this.processRequestData(mergedConfig);
6733
6747
  var builtUrl = this.buildUrl(mergedConfig);
6734
6748
  var injectedHeaders = this.injectHeaders(mergedConfig);
6735
- ctx.setParameters(_object_spread_props(_object_spread({}, ctx.parameters, mergedConfig), {
6749
+ return Object.assign(mergedConfig, {
6736
6750
  data: processedData,
6737
6751
  url: builtUrl,
6738
6752
  headers: injectedHeaders
6739
- }));
6753
+ });
6740
6754
  }
6741
6755
  },
6742
6756
  {
@@ -6751,9 +6765,9 @@ var qloverFeCorekit = function() {
6751
6765
  *
6752
6766
  * @param contextConfig - Configuration from request context
6753
6767
  * @returns Merged configuration
6754
- */ key: "mergeConfig",
6755
- value: function mergeConfig(contextConfig) {
6756
- var merged = _object_spread({}, this.config, contextConfig);
6768
+ */ key: "createConfig",
6769
+ value: function createConfig(contextConfig) {
6770
+ var merged = Object.assign(clone_default(this.config), contextConfig);
6757
6771
  if (!("data" in contextConfig) && "data" in this.config) {
6758
6772
  merged.data = this.config.data;
6759
6773
  }
@@ -6769,12 +6783,10 @@ var qloverFeCorekit = function() {
6769
6783
  * @throws {Error} If the built URL is empty or invalid
6770
6784
  */ key: "buildUrl",
6771
6785
  value: function buildUrl(config) {
6772
- var url = this.urlBuilder.buildUrl(config);
6773
- if (!url || url.trim() === "") {
6774
- var _config_baseURL, _config_url;
6775
- throw new Error("RequestPlugin: Invalid URL. URL cannot be empty. baseURL: ".concat((_config_baseURL = config.baseURL) !== null && _config_baseURL !== void 0 ? _config_baseURL : "undefined", ", url: ").concat((_config_url = config.url) !== null && _config_url !== void 0 ? _config_url : "undefined"));
6786
+ if (config.allowEmptyUrl === false && !config.url && !config.baseURL) {
6787
+ throw new Error("Empty URL is not allowed");
6776
6788
  }
6777
- return url;
6789
+ return this.urlBuilder.buildUrl(config);
6778
6790
  }
6779
6791
  },
6780
6792
  {
@@ -6919,42 +6931,21 @@ var qloverFeCorekit = function() {
6919
6931
  */ function onSuccess(context) {
6920
6932
  var _this = this;
6921
6933
  return _async_to_generator(function() {
6922
- var returnValue, config, processedResponse, processedResponse1;
6934
+ var returnValue, config, result;
6923
6935
  return _ts_generator(this, function(_state) {
6924
6936
  switch(_state.label){
6925
6937
  case 0:
6926
6938
  returnValue = context.returnValue;
6927
6939
  config = context.parameters;
6928
- if (!_instanceof(returnValue, Response)) return [
6929
- 3,
6930
- 2
6931
- ];
6932
6940
  return [
6933
6941
  4,
6934
- _this.processResponse(returnValue, config)
6942
+ _this.handleResponse(returnValue, config)
6935
6943
  ];
6936
6944
  case 1:
6937
- processedResponse = _state.sent();
6938
- context.setReturnValue(processedResponse);
6939
- return [
6940
- 2
6941
- ];
6942
- case 2:
6943
- if (!isRequestAdapterResponse(returnValue)) return [
6944
- 3,
6945
- 4
6946
- ];
6947
- return [
6948
- 4,
6949
- _this.processAdapterResponse(returnValue, config)
6950
- ];
6951
- case 3:
6952
- processedResponse1 = _state.sent();
6953
- context.setReturnValue(processedResponse1);
6954
- return [
6955
- 2
6956
- ];
6957
- case 4:
6945
+ result = _state.sent();
6946
+ if (result) {
6947
+ context.setReturnValue(result);
6948
+ }
6958
6949
  return [
6959
6950
  2
6960
6951
  ];
@@ -6963,6 +6954,17 @@ var qloverFeCorekit = function() {
6963
6954
  })();
6964
6955
  }
6965
6956
  },
6957
+ {
6958
+ key: "handleResponse",
6959
+ value: function handleResponse(returnValue, config) {
6960
+ if (_instanceof(returnValue, Response)) {
6961
+ return this.processResponse(returnValue, config);
6962
+ }
6963
+ if (isRequestAdapterResponse(returnValue)) {
6964
+ return this.processAdapterResponse(returnValue, config);
6965
+ }
6966
+ }
6967
+ },
6966
6968
  {
6967
6969
  /**
6968
6970
  * Validate response status
package/dist/index.js CHANGED
@@ -3909,6 +3909,7 @@ var SimpleUrlBuilder = class {
3909
3909
  };
3910
3910
 
3911
3911
  // src/request/impl/RequestPlugin.ts
3912
+ import { clone as clone2 } from "lodash-es";
3912
3913
  var RequestPlugin = class {
3913
3914
  pluginName = "RequestPlugin";
3914
3915
  config;
@@ -3963,13 +3964,23 @@ var RequestPlugin = class {
3963
3964
  * @override
3964
3965
  */
3965
3966
  onBefore(ctx) {
3966
- const mergedConfig = this.mergeConfig(ctx.parameters);
3967
+ ctx.setParameters(this.mergeConfig(ctx.parameters));
3968
+ }
3969
+ /**
3970
+ * Main request handler
3971
+ *
3972
+ * This is the core of the plugin. It merges default plugin configuration with request context configuration,
3973
+ * processes request data, builds the URL, and injects headers.
3974
+ *
3975
+ * @param config - Request configuration
3976
+ * @returns Merged configuration with processed data, built URL, and injected headers
3977
+ */
3978
+ mergeConfig(config) {
3979
+ const mergedConfig = this.createConfig(config);
3967
3980
  const processedData = this.processRequestData(mergedConfig);
3968
3981
  const builtUrl = this.buildUrl(mergedConfig);
3969
3982
  const injectedHeaders = this.injectHeaders(mergedConfig);
3970
- ctx.setParameters({
3971
- ...ctx.parameters,
3972
- ...mergedConfig,
3983
+ return Object.assign(mergedConfig, {
3973
3984
  data: processedData,
3974
3985
  url: builtUrl,
3975
3986
  headers: injectedHeaders
@@ -3987,8 +3998,8 @@ var RequestPlugin = class {
3987
3998
  * @param contextConfig - Configuration from request context
3988
3999
  * @returns Merged configuration
3989
4000
  */
3990
- mergeConfig(contextConfig) {
3991
- const merged = { ...this.config, ...contextConfig };
4001
+ createConfig(contextConfig) {
4002
+ const merged = Object.assign(clone2(this.config), contextConfig);
3992
4003
  if (!("data" in contextConfig) && "data" in this.config) {
3993
4004
  merged.data = this.config.data;
3994
4005
  }
@@ -4002,13 +4013,10 @@ var RequestPlugin = class {
4002
4013
  * @throws {Error} If the built URL is empty or invalid
4003
4014
  */
4004
4015
  buildUrl(config) {
4005
- const url = this.urlBuilder.buildUrl(config);
4006
- if (!url || url.trim() === "") {
4007
- throw new Error(
4008
- `RequestPlugin: Invalid URL. URL cannot be empty. baseURL: ${config.baseURL ?? "undefined"}, url: ${config.url ?? "undefined"}`
4009
- );
4016
+ if (config.allowEmptyUrl === false && !config.url && !config.baseURL) {
4017
+ throw new Error("Empty URL is not allowed");
4010
4018
  }
4011
- return url;
4019
+ return this.urlBuilder.buildUrl(config);
4012
4020
  }
4013
4021
  /**
4014
4022
  * Inject default headers into request configuration
@@ -4143,18 +4151,17 @@ var ResponsePlugin = class {
4143
4151
  async onSuccess(context) {
4144
4152
  const returnValue = context.returnValue;
4145
4153
  const config = context.parameters;
4154
+ const result = await this.handleResponse(returnValue, config);
4155
+ if (result) {
4156
+ context.setReturnValue(result);
4157
+ }
4158
+ }
4159
+ handleResponse(returnValue, config) {
4146
4160
  if (returnValue instanceof Response) {
4147
- const processedResponse = await this.processResponse(returnValue, config);
4148
- context.setReturnValue(processedResponse);
4149
- return;
4161
+ return this.processResponse(returnValue, config);
4150
4162
  }
4151
4163
  if (isRequestAdapterResponse(returnValue)) {
4152
- const processedResponse = await this.processAdapterResponse(
4153
- returnValue,
4154
- config
4155
- );
4156
- context.setReturnValue(processedResponse);
4157
- return;
4164
+ return this.processAdapterResponse(returnValue, config);
4158
4165
  }
4159
4166
  }
4160
4167
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qlover/fe-corekit",
3
3
  "description": "A corekit for frontwork",
4
- "version": "3.1.0",
4
+ "version": "3.1.1",
5
5
  "private": false,
6
6
  "type": "module",
7
7
  "files": [