@sevtech/kit 21.6.2 → 21.8.0

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/fesm2022/http.mjs CHANGED
@@ -22,6 +22,7 @@ const API_OPTIONS = new InjectionToken('[API_OPTIONS]: определяет на
22
22
  factory: () => DEFAULT_API_OPTIONS,
23
23
  });
24
24
  const API_BODY_TRANSFORM = new InjectionToken('[API_BODY_TRANSFORM]: предоставляет функции для трансформации тела запроса');
25
+ const API_REQUEST_CONTEXT = new InjectionToken('[API_REQUEST_CONTEXT]: задаёт токены контекста по умолчанию');
25
26
  const API_SERIALIZE = new InjectionToken('[API_SERIALIZE]: конфигурация сериализации параметров и тела запроса', {
26
27
  providedIn: 'root',
27
28
  factory: () => DEFAULT_SERIALIZE_CONFIG,
@@ -52,6 +53,7 @@ class ApiBase {
52
53
  this.serialize = inject(API_SERIALIZE);
53
54
  this.prefix = inject(API_PREFIX, { optional: true });
54
55
  this.options = inject(API_OPTIONS);
56
+ this.defaultContext = inject(API_REQUEST_CONTEXT, { optional: true });
55
57
  }
56
58
  request(method, url, options) {
57
59
  const { contentType, body, serialize, ...restOptions } = this.resolveRequestOptions(options);
@@ -107,7 +109,7 @@ class ApiBase {
107
109
  }
108
110
  resolveOptions(options, serializeConfig) {
109
111
  const params = normalizeParams(options.params, serializeConfig);
110
- const context = normalizeContext(options.context);
112
+ const context = normalizeContext(options.context, this.defaultContext);
111
113
  return {
112
114
  ...options,
113
115
  context,
@@ -157,8 +159,10 @@ function normalizeFormDataContent(data, config) {
157
159
  }
158
160
  return formData;
159
161
  }
160
- function normalizeContext(context) {
161
- return isFunction(context) ? context(new HttpContext()) : undefined;
162
+ function normalizeContext(requestContext, defaultContext) {
163
+ const initialContext = new HttpContext();
164
+ const context = isFunction(defaultContext) ? defaultContext(initialContext) : initialContext;
165
+ return isFunction(requestContext) ? requestContext(context) : context;
162
166
  }
163
167
  function normalizeParams(params, config) {
164
168
  if (isNil(params))
@@ -320,6 +324,17 @@ const withSerializeConfig = defineKitFeature(2 /* FeatureKind.Serialize */, (con
320
324
  provide: API_SERIALIZE,
321
325
  useFactory: () => ({ ...DEFAULT_SERIALIZE_CONFIG, ...resolveMaybeFn(config) }),
322
326
  }));
327
+ /**
328
+ * Задаёт токены контекста по умолчанию для запросов через `HttpApiService`.
329
+ * @description Callback получает новый контекст для каждого запроса.
330
+ * `options.context` применяется после него и может переопределить значения токенов.
331
+ * @example
332
+ * provideHttpApi(withDefaultContext((ctx) => ctx.set(USE_HEADERS, true)));
333
+ */
334
+ const withDefaultContext = defineKitFeature(3 /* FeatureKind.DefaultContext */, (context) => ({
335
+ provide: API_REQUEST_CONTEXT,
336
+ useValue: context,
337
+ }));
323
338
  function provideHttpApi(options, ...features) {
324
339
  const providers = resolveConfigProviders(options, (item) => [
325
340
  {
@@ -1639,5 +1654,5 @@ const timeoutInterceptor = (request, next) => {
1639
1654
  * Generated bundle index. Do not edit.
1640
1655
  */
1641
1656
 
1642
- export { ApiError, ErrorKind as ApiErrorKind, HttpApiService, ProgressHttpService, USE_ERROR_MESSAGE, USE_HEADERS, USE_LEGACY_ERRORS, USE_LOGGER, USE_MOCK, USE_PROGRESS, USE_RETRY, USE_TIMEOUT, defineMock, headersInterceptor, httpLogger, isApiError, loggerInterceptor, mockInterceptor, openApiInterceptor, progressInterceptor, provideHeadersConfig, provideHttpApi, provideLoggerConfig, provideMockConfig, provideOpenApiConfig, provideProgressConfig, provideRetryConfig, provideTimeoutConfig, retryInterceptor, timeoutInterceptor, withErrorMessages as withApiErrorMessages, withKindResolver as withApiKindResolver, withApiResponseFilter, withBodyTransform, withCustomHandler as withCustomLogger, withHeaders, withHeadersResolver, withPrefix, withSerializeConfig };
1657
+ export { ApiError, ErrorKind as ApiErrorKind, HttpApiService, ProgressHttpService, USE_ERROR_MESSAGE, USE_HEADERS, USE_LEGACY_ERRORS, USE_LOGGER, USE_MOCK, USE_PROGRESS, USE_RETRY, USE_TIMEOUT, defineMock, headersInterceptor, httpLogger, isApiError, loggerInterceptor, mockInterceptor, openApiInterceptor, progressInterceptor, provideHeadersConfig, provideHttpApi, provideLoggerConfig, provideMockConfig, provideOpenApiConfig, provideProgressConfig, provideRetryConfig, provideTimeoutConfig, retryInterceptor, timeoutInterceptor, withErrorMessages as withApiErrorMessages, withKindResolver as withApiKindResolver, withApiResponseFilter, withBodyTransform, withCustomHandler as withCustomLogger, withDefaultContext, withHeaders, withHeadersResolver, withPrefix, withSerializeConfig };
1643
1658
  //# sourceMappingURL=http.mjs.map