@monetizationos/proxy 1.2.0 → 1.3.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.
@@ -3,7 +3,7 @@ import type { Fetcher } from './adapters/Fetcher';
3
3
  import type { HtmlRewriterAdapter } from './adapters/HtmlRewriterAdapter';
4
4
  import type { IdentityProvider } from './adapters/IdentityProvider';
5
5
  import { type MOSProxyLogger } from './logger';
6
- import type { MOSConfigInput } from './types';
6
+ import type { MOSConfigInput, MosAuthenticatedApiRoute } from './types';
7
7
  export type MOSProxyHtmlPipelineStage = 'origin-response' | 'link-rewriting' | 'surface-decisions' | 'surface-behavior' | 'surface-components';
8
8
  export interface MOSProxyHtmlPipelineErrorContext {
9
9
  error: unknown;
@@ -32,6 +32,7 @@ export interface MOSProxyOptions {
32
32
  linkRewritingEnabled: boolean;
33
33
  surfaceDecisionsEnabled: boolean;
34
34
  htmlTransformationEnabled: boolean;
35
+ mosAuthenticatedApiRoutes?: MosAuthenticatedApiRoute[];
35
36
  }
36
37
  /**
37
38
  * Platform-agnostic MonetizationOS proxy. Build one with `MOSProxyBuilder`, then call
package/dist/MOSProxy.js CHANGED
@@ -3,6 +3,7 @@ import { consoleLogger } from './logger';
3
3
  import customEndpointRequest from './stages/customEndpoint';
4
4
  import isRedirectResponse from './stages/isRedirectResponse';
5
5
  import rewriteOriginResponse from './stages/linkRewriting';
6
+ import { handleMosAuthenticatedApiRoutes } from './stages/mosAuthenticatedApi';
6
7
  import performOriginRequest from './stages/originRequest';
7
8
  import shouldIgnorePath from './stages/shouldIgnorePath';
8
9
  import handleSurfaceBehavior from './stages/surfaceBehavior';
@@ -33,7 +34,12 @@ export class MOSProxy {
33
34
  return customEndpointResponse;
34
35
  }
35
36
  }
36
- // Stage 1b: origin fetch
37
+ // Stage 1b: authenticated API routing
38
+ const authenticatedApiResult = await handleMosAuthenticatedApiRoutes(request, this.opts.mosAuthenticatedApiRoutes ?? [], ctx, identityProvider, clientMetadataProvider, apiFetcher);
39
+ if (authenticatedApiResult) {
40
+ return authenticatedApiResult;
41
+ }
42
+ // Stage 1c: origin fetch
37
43
  const originResponse = await performOriginRequest(ctx, request, originFetcher);
38
44
  // Auto-skip HTML pipeline for non-HTML content, or when HTML transformation is disabled entirely
39
45
  const isHtml = originResponse.headers.get('Content-Type')?.startsWith('text/html') ?? false;
@@ -1 +1 @@
1
- {"version":3,"file":"MOSProxy.js","sourceRoot":"","sources":["../src/MOSProxy.ts"],"names":[],"mappings":"AAIA,OAAO,EAAkB,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAE7D,OAAO,EAAE,aAAa,EAAuB,MAAM,UAAU,CAAA;AAC7D,OAAO,qBAAqB,MAAM,yBAAyB,CAAA;AAC3D,OAAO,kBAAkB,MAAM,6BAA6B,CAAA;AAC5D,OAAO,qBAAqB,MAAM,wBAAwB,CAAA;AAC1D,OAAO,oBAAoB,MAAM,wBAAwB,CAAA;AACzD,OAAO,gBAAgB,MAAM,2BAA2B,CAAA;AACxD,OAAO,qBAAqB,MAAM,0BAA0B,CAAA;AAC5D,OAAO,uBAAuB,MAAM,4BAA4B,CAAA;AAChE,OAAO,mBAAmB,MAAM,2BAA2B,CAAA;AAyC3D;;;GAGG;AACH,MAAM,OAAO,QAAQ;IAGY;IAFZ,MAAM,CAAW;IAElC,YAA6B,IAAqB;QAArB,SAAI,GAAJ,IAAI,CAAiB;QAC9C,IAAI,CAAC,MAAM,GAAG,kBAAkB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,aAAa,CAAC,CAAA;IAC/E,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,OAAgB;QACzB,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,YAAY,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,GAAG,IAAI,CAAC,IAAI,CAAA;QAC5H,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,aAAa,CAAA;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IAC3B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,MAAM,CAAC,GAAG,CAAC;YACP,KAAK,EAAE,MAAM;YACb,IAAI,EAAE,uBAAuB;YAC7B,OAAO,EAAE,kDAAkD;YAC3D,OAAO,EAAE,EAAE,KAAK,EAAE;YAClB,KAAK;SACR,CAAC,CAAA;QACF,OAAO,QAAQ,CAAA;IACnB,CAAC;AACL,CAAC,CAAA","sourcesContent":["import type { ClientMetadataProvider } from './adapters/ClientMetadataProvider'\nimport type { Fetcher } from './adapters/Fetcher'\nimport type { HtmlRewriterAdapter } from './adapters/HtmlRewriterAdapter'\nimport type { IdentityProvider } from './adapters/IdentityProvider'\nimport { type MOSConfig, normalizeMOSConfig } from './config'\nimport type { PipelineContext } from './context'\nimport { consoleLogger, type MOSProxyLogger } from './logger'\nimport customEndpointRequest from './stages/customEndpoint'\nimport isRedirectResponse from './stages/isRedirectResponse'\nimport rewriteOriginResponse from './stages/linkRewriting'\nimport performOriginRequest from './stages/originRequest'\nimport shouldIgnorePath from './stages/shouldIgnorePath'\nimport handleSurfaceBehavior from './stages/surfaceBehavior'\nimport handleSurfaceComponents from './stages/surfaceComponents'\nimport getSurfaceDecisions from './stages/surfaceDecisions'\nimport type { MOSConfigInput } from './types'\n\nexport type MOSProxyHtmlPipelineStage =\n | 'origin-response'\n | 'link-rewriting'\n | 'surface-decisions'\n | 'surface-behavior'\n | 'surface-components'\n\nexport interface MOSProxyHtmlPipelineErrorContext {\n error: unknown\n stage: MOSProxyHtmlPipelineStage\n request: Request\n lastSafeResponse: Response\n}\n\n/**\n * Called when the HTML pipeline throws. Return a `Response` to fully control the proxy's reply, or\n * re-throw to surface the error to the platform handler. If omitted, the proxy fails open and\n * returns `lastSafeResponse`. If the callback itself throws or returns a non-Response value, the\n * proxy logs a warning and falls back to `lastSafeResponse` so the site cannot be taken down by a\n * bug in the handler.\n */\nexport type MOSProxyHtmlPipelineErrorHandler = (ctx: MOSProxyHtmlPipelineErrorContext) => Response | Promise<Response>\n\nexport interface MOSProxyOptions {\n config: MOSConfigInput\n originFetcher: Fetcher\n apiFetcher: Fetcher | null\n htmlRewriter: HtmlRewriterAdapter | null\n clientMetadataProvider: ClientMetadataProvider | null\n identityProvider: IdentityProvider | null\n logger?: MOSProxyLogger\n onHtmlPipelineError?: MOSProxyHtmlPipelineErrorHandler\n customEndpointsEnabled: boolean\n linkRewritingEnabled: boolean\n surfaceDecisionsEnabled: boolean\n htmlTransformationEnabled: boolean\n}\n\n/**\n * Platform-agnostic MonetizationOS proxy. Build one with `MOSProxyBuilder`, then call\n * `proxy.handle(request)` from your platform's fetch entry point.\n */\nexport class MOSProxy {\n private readonly config: MOSConfig\n\n constructor(private readonly opts: MOSProxyOptions) {\n this.config = normalizeMOSConfig(opts.config, opts.logger ?? consoleLogger)\n }\n\n async handle(request: Request): Promise<Response> {\n const { originFetcher, apiFetcher, htmlRewriter, clientMetadataProvider, identityProvider, onHtmlPipelineError } = this.opts\n const logger = this.opts.logger ?? consoleLogger\n const ctx: PipelineContext = { config: this.config, logger }\n\n // Stage 1a: custom endpoint routing\n if (this.opts.customEndpointsEnabled) {\n if (!apiFetcher) {\n throw new Error('MOSProxy: customEndpoints is enabled but no API fetcher is configured')\n }\n const customEndpointResponse = await customEndpointRequest(ctx, request, apiFetcher)\n if (customEndpointResponse) {\n return customEndpointResponse\n }\n }\n\n // Stage 1b: origin fetch\n const originResponse = await performOriginRequest(ctx, request, originFetcher)\n\n // Auto-skip HTML pipeline for non-HTML content, or when HTML transformation is disabled entirely\n const isHtml = originResponse.headers.get('Content-Type')?.startsWith('text/html') ?? false\n if (!isHtml || !this.opts.htmlTransformationEnabled) {\n return originResponse\n }\n\n let stage: MOSProxyHtmlPipelineStage = 'origin-response'\n let failOpenResponse = cloneResponseForFallback(originResponse, logger, stage)\n\n try {\n // Stage 2: rewrite origin links (optional)\n let rewrittenResponse: Response = originResponse\n if (this.opts.linkRewritingEnabled) {\n stage = 'link-rewriting'\n rewrittenResponse = await rewriteOriginResponse(ctx, request, originResponse)\n failOpenResponse = cloneResponseForFallback(rewrittenResponse, logger, stage)\n }\n\n // Stage 3: surface decisions (optional)\n if (!this.opts.surfaceDecisionsEnabled) {\n return rewrittenResponse\n }\n if (shouldIgnorePath(ctx, request) || isRedirectResponse(rewrittenResponse)) {\n return rewrittenResponse\n }\n\n if (!apiFetcher) {\n throw new Error('MOSProxy: surfaceDecisions is enabled but no API fetcher is configured')\n }\n\n stage = 'surface-decisions'\n const [modifiedResponse, surfaceDecisions] = await getSurfaceDecisions(\n ctx,\n request,\n rewrittenResponse,\n apiFetcher,\n htmlRewriter,\n clientMetadataProvider,\n identityProvider,\n )\n if (!surfaceDecisions) {\n return modifiedResponse\n }\n failOpenResponse = cloneResponseForFallback(modifiedResponse, logger, stage)\n\n // Stage 4: surface behavior (HTTP-level)\n stage = 'surface-behavior'\n const [surfaceDecisionResponse, returnImmediately] = handleSurfaceBehavior(modifiedResponse, surfaceDecisions)\n if (returnImmediately) {\n return surfaceDecisionResponse\n }\n failOpenResponse = cloneResponseForFallback(surfaceDecisionResponse, logger, stage)\n\n // Stage 5: surface components (HTML-level) — requires an HTML rewriter\n if (!htmlRewriter) {\n return surfaceDecisionResponse\n }\n stage = 'surface-components'\n return await handleSurfaceComponents(ctx, surfaceDecisionResponse, surfaceDecisions, htmlRewriter)\n } catch (error) {\n logger.log({\n level: 'error',\n code: 'html-pipeline-failed',\n message: 'HTML pipeline failed.',\n context: { stage, hasErrorHandler: Boolean(onHtmlPipelineError) },\n error,\n })\n\n if (!onHtmlPipelineError) {\n return failOpenResponse\n }\n\n try {\n const handlerResponse = await onHtmlPipelineError({\n error,\n stage,\n request,\n lastSafeResponse: failOpenResponse,\n })\n if (!(handlerResponse instanceof Response)) {\n logger.log({\n level: 'warn',\n code: 'html-pipeline-error-handler-invalid',\n message: 'onHtmlPipelineError did not return a Response; falling back to the last safe response.',\n context: { stage },\n })\n return failOpenResponse\n }\n return handlerResponse\n } catch (handlerError) {\n logger.log({\n level: 'warn',\n code: 'html-pipeline-error-handler-threw',\n message: 'onHtmlPipelineError threw; falling back to the last safe response.',\n context: { stage },\n error: handlerError,\n })\n return failOpenResponse\n }\n }\n }\n}\n\nconst cloneResponseForFallback = (response: Response, logger: MOSProxyLogger, stage: MOSProxyHtmlPipelineStage): Response => {\n try {\n return response.clone()\n } catch (error) {\n logger.log({\n level: 'warn',\n code: 'response-clone-failed',\n message: 'Could not clone response for fail-open fallback.',\n context: { stage },\n error,\n })\n return response\n }\n}\n"]}
1
+ {"version":3,"file":"MOSProxy.js","sourceRoot":"","sources":["../src/MOSProxy.ts"],"names":[],"mappings":"AAIA,OAAO,EAAkB,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAE7D,OAAO,EAAE,aAAa,EAAuB,MAAM,UAAU,CAAA;AAC7D,OAAO,qBAAqB,MAAM,yBAAyB,CAAA;AAC3D,OAAO,kBAAkB,MAAM,6BAA6B,CAAA;AAC5D,OAAO,qBAAqB,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,+BAA+B,EAAE,MAAM,8BAA8B,CAAA;AAC9E,OAAO,oBAAoB,MAAM,wBAAwB,CAAA;AACzD,OAAO,gBAAgB,MAAM,2BAA2B,CAAA;AACxD,OAAO,qBAAqB,MAAM,0BAA0B,CAAA;AAC5D,OAAO,uBAAuB,MAAM,4BAA4B,CAAA;AAChE,OAAO,mBAAmB,MAAM,2BAA2B,CAAA;AA0C3D;;;GAGG;AACH,MAAM,OAAO,QAAQ;IAGY;IAFZ,MAAM,CAAW;IAElC,YAA6B,IAAqB;QAArB,SAAI,GAAJ,IAAI,CAAiB;QAC9C,IAAI,CAAC,MAAM,GAAG,kBAAkB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,aAAa,CAAC,CAAA;IAC/E,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,OAAgB;QACzB,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,YAAY,EAAE,sBAAsB,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M,CAAC,GAAG,CAAC;oBACP,KAAK,EAAE,MAAM;oBACb,IAAI,EAAE,mCAAmC;oBACzC,OAAO,EAAE,oEAAoE;oBAC7E,OAAO,EAAE,EAAE,KAAK,EAAE;oBAClB,KAAK,EAAE,YAAY;iBACtB,CAAC,CAAA;gBACF,OAAO,gBAAgB,CAAA;YAC3B,CAAC;QACL,CAAC;IACL,CAAC;CACJ;AAED,MAAM,wBAAwB,GAAG,CAAC,QAAkB,EAAE,MAAsB,EAAE,KAAgC,EAAY,EAAE;IACxH,IAAI,CAAC;QACD,OAAO,QAAQ,CAAC,KAAK,EAAE,CAAA;IAC3B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,MAAM,CAAC,GAAG,CAAC;YACP,KAAK,EAAE,MAAM;YACb,IAAI,EAAE,uBAAuB;YAC7B,OAAO,EAAE,kDAAkD;YAC3D,OAAO,EAAE,EAAE,KAAK,EAAE;YAClB,KAAK;SACR,CAAC,CAAA;QACF,OAAO,QAAQ,CAAA;IACnB,CAAC;AACL,CAAC,CAAA","sourcesContent":["import type { ClientMetadataProvider } from './adapters/ClientMetadataProvider'\nimport type { Fetcher } from './adapters/Fetcher'\nimport type { HtmlRewriterAdapter } from './adapters/HtmlRewriterAdapter'\nimport type { IdentityProvider } from './adapters/IdentityProvider'\nimport { type MOSConfig, normalizeMOSConfig } from './config'\nimport type { PipelineContext } from './context'\nimport { consoleLogger, type MOSProxyLogger } from './logger'\nimport customEndpointRequest from './stages/customEndpoint'\nimport isRedirectResponse from './stages/isRedirectResponse'\nimport rewriteOriginResponse from './stages/linkRewriting'\nimport { handleMosAuthenticatedApiRoutes } from './stages/mosAuthenticatedApi'\nimport performOriginRequest from './stages/originRequest'\nimport shouldIgnorePath from './stages/shouldIgnorePath'\nimport handleSurfaceBehavior from './stages/surfaceBehavior'\nimport handleSurfaceComponents from './stages/surfaceComponents'\nimport getSurfaceDecisions from './stages/surfaceDecisions'\nimport type { MOSConfigInput, MosAuthenticatedApiRoute } from './types'\n\nexport type MOSProxyHtmlPipelineStage =\n | 'origin-response'\n | 'link-rewriting'\n | 'surface-decisions'\n | 'surface-behavior'\n | 'surface-components'\n\nexport interface MOSProxyHtmlPipelineErrorContext {\n error: unknown\n stage: MOSProxyHtmlPipelineStage\n request: Request\n lastSafeResponse: Response\n}\n\n/**\n * Called when the HTML pipeline throws. Return a `Response` to fully control the proxy's reply, or\n * re-throw to surface the error to the platform handler. If omitted, the proxy fails open and\n * returns `lastSafeResponse`. If the callback itself throws or returns a non-Response value, the\n * proxy logs a warning and falls back to `lastSafeResponse` so the site cannot be taken down by a\n * bug in the handler.\n */\nexport type MOSProxyHtmlPipelineErrorHandler = (ctx: MOSProxyHtmlPipelineErrorContext) => Response | Promise<Response>\n\nexport interface MOSProxyOptions {\n config: MOSConfigInput\n originFetcher: Fetcher\n apiFetcher: Fetcher | null\n htmlRewriter: HtmlRewriterAdapter | null\n clientMetadataProvider: ClientMetadataProvider | null\n identityProvider: IdentityProvider | null\n logger?: MOSProxyLogger\n onHtmlPipelineError?: MOSProxyHtmlPipelineErrorHandler\n customEndpointsEnabled: boolean\n linkRewritingEnabled: boolean\n surfaceDecisionsEnabled: boolean\n htmlTransformationEnabled: boolean\n mosAuthenticatedApiRoutes?: MosAuthenticatedApiRoute[]\n}\n\n/**\n * Platform-agnostic MonetizationOS proxy. Build one with `MOSProxyBuilder`, then call\n * `proxy.handle(request)` from your platform's fetch entry point.\n */\nexport class MOSProxy {\n private readonly config: MOSConfig\n\n constructor(private readonly opts: MOSProxyOptions) {\n this.config = normalizeMOSConfig(opts.config, opts.logger ?? consoleLogger)\n }\n\n async handle(request: Request): Promise<Response> {\n const { originFetcher, apiFetcher, htmlRewriter, clientMetadataProvider, identityProvider, onHtmlPipelineError } = this.opts\n const logger = this.opts.logger ?? consoleLogger\n const ctx: PipelineContext = { config: this.config, logger }\n\n // Stage 1a: custom endpoint routing\n if (this.opts.customEndpointsEnabled) {\n if (!apiFetcher) {\n throw new Error('MOSProxy: customEndpoints is enabled but no API fetcher is configured')\n }\n const customEndpointResponse = await customEndpointRequest(ctx, request, apiFetcher)\n if (customEndpointResponse) {\n return customEndpointResponse\n }\n }\n\n // Stage 1b: authenticated API routing\n const authenticatedApiResult = await handleMosAuthenticatedApiRoutes(\n request,\n this.opts.mosAuthenticatedApiRoutes ?? [],\n ctx,\n identityProvider,\n clientMetadataProvider,\n apiFetcher,\n )\n if (authenticatedApiResult) {\n return authenticatedApiResult\n }\n\n // Stage 1c: origin fetch\n const originResponse = await performOriginRequest(ctx, request, originFetcher)\n\n // Auto-skip HTML pipeline for non-HTML content, or when HTML transformation is disabled entirely\n const isHtml = originResponse.headers.get('Content-Type')?.startsWith('text/html') ?? false\n if (!isHtml || !this.opts.htmlTransformationEnabled) {\n return originResponse\n }\n\n let stage: MOSProxyHtmlPipelineStage = 'origin-response'\n let failOpenResponse = cloneResponseForFallback(originResponse, logger, stage)\n\n try {\n // Stage 2: rewrite origin links (optional)\n let rewrittenResponse: Response = originResponse\n if (this.opts.linkRewritingEnabled) {\n stage = 'link-rewriting'\n rewrittenResponse = await rewriteOriginResponse(ctx, request, originResponse)\n failOpenResponse = cloneResponseForFallback(rewrittenResponse, logger, stage)\n }\n\n // Stage 3: surface decisions (optional)\n if (!this.opts.surfaceDecisionsEnabled) {\n return rewrittenResponse\n }\n if (shouldIgnorePath(ctx, request) || isRedirectResponse(rewrittenResponse)) {\n return rewrittenResponse\n }\n\n if (!apiFetcher) {\n throw new Error('MOSProxy: surfaceDecisions is enabled but no API fetcher is configured')\n }\n\n stage = 'surface-decisions'\n const [modifiedResponse, surfaceDecisions] = await getSurfaceDecisions(\n ctx,\n request,\n rewrittenResponse,\n apiFetcher,\n htmlRewriter,\n clientMetadataProvider,\n identityProvider,\n )\n if (!surfaceDecisions) {\n return modifiedResponse\n }\n failOpenResponse = cloneResponseForFallback(modifiedResponse, logger, stage)\n\n // Stage 4: surface behavior (HTTP-level)\n stage = 'surface-behavior'\n const [surfaceDecisionResponse, returnImmediately] = handleSurfaceBehavior(modifiedResponse, surfaceDecisions)\n if (returnImmediately) {\n return surfaceDecisionResponse\n }\n failOpenResponse = cloneResponseForFallback(surfaceDecisionResponse, logger, stage)\n\n // Stage 5: surface components (HTML-level) — requires an HTML rewriter\n if (!htmlRewriter) {\n return surfaceDecisionResponse\n }\n stage = 'surface-components'\n return await handleSurfaceComponents(ctx, surfaceDecisionResponse, surfaceDecisions, htmlRewriter)\n } catch (error) {\n logger.log({\n level: 'error',\n code: 'html-pipeline-failed',\n message: 'HTML pipeline failed.',\n context: { stage, hasErrorHandler: Boolean(onHtmlPipelineError) },\n error,\n })\n\n if (!onHtmlPipelineError) {\n return failOpenResponse\n }\n\n try {\n const handlerResponse = await onHtmlPipelineError({\n error,\n stage,\n request,\n lastSafeResponse: failOpenResponse,\n })\n if (!(handlerResponse instanceof Response)) {\n logger.log({\n level: 'warn',\n code: 'html-pipeline-error-handler-invalid',\n message: 'onHtmlPipelineError did not return a Response; falling back to the last safe response.',\n context: { stage },\n })\n return failOpenResponse\n }\n return handlerResponse\n } catch (handlerError) {\n logger.log({\n level: 'warn',\n code: 'html-pipeline-error-handler-threw',\n message: 'onHtmlPipelineError threw; falling back to the last safe response.',\n context: { stage },\n error: handlerError,\n })\n return failOpenResponse\n }\n }\n }\n}\n\nconst cloneResponseForFallback = (response: Response, logger: MOSProxyLogger, stage: MOSProxyHtmlPipelineStage): Response => {\n try {\n return response.clone()\n } catch (error) {\n logger.log({\n level: 'warn',\n code: 'response-clone-failed',\n message: 'Could not clone response for fail-open fallback.',\n context: { stage },\n error,\n })\n return response\n }\n}\n"]}
@@ -4,7 +4,7 @@ import type { HtmlRewriterAdapter } from './adapters/HtmlRewriterAdapter';
4
4
  import type { IdentityProvider } from './adapters/IdentityProvider';
5
5
  import type { MOSProxyLogger } from './logger';
6
6
  import { MOSProxy, type MOSProxyHtmlPipelineErrorHandler } from './MOSProxy';
7
- import type { MOSConfigInput } from './types';
7
+ import type { MOSConfigInput, MosAuthenticatedApiRoute } from './types';
8
8
  /**
9
9
  * Fluent builder for `MOSProxy`. Provide configuration and platform adapters, then `build()`.
10
10
  *
@@ -50,6 +50,7 @@ export declare class MOSProxyBuilder {
50
50
  private _linkRewriting;
51
51
  private _surfaceDecisions;
52
52
  private _htmlTransformation;
53
+ private _additionalAuthenticatedApiRoutes;
53
54
  withConfig(config: MOSConfigInput): this;
54
55
  withOriginFetcher(fetcher: Fetcher): this;
55
56
  withApiFetcher(fetcher: Fetcher): this;
@@ -82,5 +83,11 @@ export declare class MOSProxyBuilder {
82
83
  * if custom endpoints and surface decisions are both disabled alongside this.
83
84
  */
84
85
  withoutHtmlTransformation(): this;
86
+ /**
87
+ * Add an additional route which, when matched by an incoming request, will be forwarded to the MonetizationOS API
88
+ * with authentication handled by the configured identity provider.
89
+ * This is in addition to the default `/mos-api/offer-redemptions` route.
90
+ */
91
+ withMosAuthenticatedApiRoutes(...routes: MosAuthenticatedApiRoute[]): this;
85
92
  build(): MOSProxy;
86
93
  }
@@ -44,6 +44,7 @@ export class MOSProxyBuilder {
44
44
  _linkRewriting = true;
45
45
  _surfaceDecisions = true;
46
46
  _htmlTransformation = true;
47
+ _additionalAuthenticatedApiRoutes = [];
47
48
  withConfig(config) {
48
49
  this._config = config;
49
50
  return this;
@@ -112,6 +113,15 @@ export class MOSProxyBuilder {
112
113
  this._htmlTransformation = false;
113
114
  return this;
114
115
  }
116
+ /**
117
+ * Add an additional route which, when matched by an incoming request, will be forwarded to the MonetizationOS API
118
+ * with authentication handled by the configured identity provider.
119
+ * This is in addition to the default `/mos-api/offer-redemptions` route.
120
+ */
121
+ withMosAuthenticatedApiRoutes(...routes) {
122
+ this._additionalAuthenticatedApiRoutes.push(...routes);
123
+ return this;
124
+ }
115
125
  build() {
116
126
  if (!this._config) {
117
127
  throw new Error('MOSProxyBuilder: withConfig(...) is required');
@@ -133,6 +143,14 @@ export class MOSProxyBuilder {
133
143
  linkRewritingEnabled: this._linkRewriting,
134
144
  surfaceDecisionsEnabled: this._surfaceDecisions,
135
145
  htmlTransformationEnabled: this._htmlTransformation,
146
+ mosAuthenticatedApiRoutes: [
147
+ ...this._additionalAuthenticatedApiRoutes,
148
+ {
149
+ matchPath: '/mos-api/offer-redemptions',
150
+ method: 'POST',
151
+ mosPath: '/api/v1/offer-redemptions',
152
+ },
153
+ ],
136
154
  };
137
155
  return new MOSProxy(opts);
138
156
  }
@@ -1 +1 @@
1
- {"version":3,"file":"MOSProxyBuilder.js","sourceRoot":"","sources":["../src/MOSProxyBuilder.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAA+D,MAAM,YAAY,CAAA;AAGlG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,OAAO,eAAe;IAChB,OAAO,GAA0B,IAAI,CAAA;IACrC,cAAc,GAAY,UAAU,CAAC,KAAK,CAAA;IAC1C,WAAW,GAAY,UAAU,CAAC,KAAK,CAAA;IACvC,aAAa,GAA+B,IAAI,CAAA;IAChD,uBAAuB,GAAkC,IAAI,CAAA;IAC7D,iBAAiB,GAA4B,IAAI,CAAA;IACjD,OAAO,GAA0B,IAAI,CAAA;IACrC,oBAAoB,GAA4C,IAAI,CAAA;IACpE,gBAAgB,GAAG,IAAI,CAAA;IACvB,cAAc,GAAG,IAAI,CAAA;IACrB,iBAAiB,GAAG,IAAI,CAAA;IACxB,mBAAmB,GAAG,IAAI,CAAA;IAElC,UAAU,CAAC,MAAsB;QAC7B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;QACrB,OAAO,IAAI,CAAA;IACf,CAAC;IAED,iBAAiB,CAAC,OAAgB;QAC9B,IAAI,CAAC,cAAc,GAAG,OAAO,CAAA;QAC7B,OAAO,IAAI,CAAA;IACf,CAAC;IAED,cAAc,CAAC,OAAgB;QAC3B,IAAI,CAAC,WAAW,GAAG,OAAO,CAAA;QAC1B,OAAO,IAAI,CAAA;IACf,CAAC;IAED,gBAAgB,CAAC,QAA6B;QAC1C,IAAI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,CAAC;QAED,MAAM,IAAI,GAAoB;YAC1B,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,aAAa,EAAE,IAAI,CAAC,cAAc;YAClC,UAAU,EAAE,IAAI,CAAC,WAAW;YAC5B,YAAY,EAAE,IAAI,CAAC,aAAa;YAChC,sBAAsB,EAAE,IAAI,CAAC,uBAAuB;YACpD,gBAAgB,EAAE,IAAI,CAAC,iBAAiB;YACxC,MAAM,EAAE,IAAI,CAAC,OAAO,IAAI,SAAS;YACjC,mBAAmB,EAAE,IAAI,CAAC,oBAAoB,IAAI,SAAS;YAC3D,sBAAsB,EAAE,IAAI,CAAC,gBAAgB;YAC7C,oBAAoB,EAAE,IAAI,CAAC,cAAc;YACzC,uBAAuB,EAAE,IAAI,CAAC,iBAAiB;YAC/C,yBAAyB,EAAE,IAAI,CAAC,mBAAmB;SACtD,CAAA;QACD,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAA;IAC7B,CAAC;CACJ","sourcesContent":["import type { ClientMetadataProvider } from './adapters/ClientMetadataProvider'\nimport type { Fetcher } from './adapters/Fetcher'\nimport type { HtmlRewriterAdapter } from './adapters/HtmlRewriterAdapter'\nimport type { IdentityProvider } from './adapters/IdentityProvider'\nimport type { MOSProxyLogger } from './logger'\nimport { MOSProxy, type MOSProxyHtmlPipelineErrorHandler, type MOSProxyOptions } from './MOSProxy'\nimport type { MOSConfigInput } from './types'\n\n/**\n * Fluent builder for `MOSProxy`. Provide configuration and platform adapters, then `build()`.\n *\n * Defaults:\n * - custom endpoint routing: enabled\n * - link rewriting: enabled\n * - surface decisions: enabled\n * - HTML transformation: enabled (auto-skipped per request for non-HTML responses)\n * - origin/API fetcher: `globalThis.fetch` (override on runtimes that need a backend binding, e.g. Fastly)\n *\n * @example\n * ```ts\n * const proxy = new MOSProxyBuilder()\n * .withConfig({\n * originUrl: 'https://news.example.com',\n * surfaceSlug: 'web',\n * mosHost: 'https://api.monetizationos.com',\n * mosSecretKey: process.env.MONETIZATION_OS_SECRET_KEY!,\n * mosEndpointsPrefix: '/mos-endpoints/',\n * anonymousSessionCookieName: 'anon-session-id',\n * authenticatedUserJwtCookieName: '__session',\n * injectScriptUrl: 'https://assets.monetizationos.com/web-components-latest.js',\n * surfaceDecisionsIgnorePaths: '',\n * originRequestHeaders: { 'X-Api-Key': process.env.ORIGIN_API_KEY! },\n * createAnonymousIdentifierFallback: true,\n * })\n * .withHtmlRewriter(myHtmlRewriterAdapter)\n * .build()\n *\n * export default { fetch: (request: Request) => proxy.handle(request) }\n * ```\n */\nexport class MOSProxyBuilder {\n private _config: MOSConfigInput | null = null\n private _originFetcher: Fetcher = globalThis.fetch\n private _apiFetcher: Fetcher = globalThis.fetch\n private _htmlRewriter: HtmlRewriterAdapter | null = null\n private _clientMetadataProvider: ClientMetadataProvider | null = null\n private _identityProvider: IdentityProvider | null = null\n private _logger: MOSProxyLogger | null = null\n private _onHtmlPipelineError: MOSProxyHtmlPipelineErrorHandler | null = null\n private _customEndpoints = true\n private _linkRewriting = true\n private _surfaceDecisions = true\n private _htmlTransformation = true\n\n withConfig(config: MOSConfigInput): this {\n this._config = config\n return this\n }\n\n withOriginFetcher(fetcher: Fetcher): this {\n this._originFetcher = fetcher\n return this\n }\n\n withApiFetcher(fetcher: Fetcher): this {\n this._apiFetcher = fetcher\n return this\n }\n\n withHtmlRewriter(rewriter: HtmlRewriterAdapter): this {\n this._htmlRewriter = rewriter\n return this\n }\n\n withClientMetadata(provider: ClientMetadataProvider): this {\n this._clientMetadataProvider = provider\n return this\n }\n\n /**\n * Override identity provision for the surface-decisions API. Provide `resolve` to control the\n * identity payload sent to the API (e.g. resolved from a request header instead of cookies),\n * and/or `persist` to control how identity is recorded back on the response (e.g. suppress the\n * default anonymous-session cookie or write it elsewhere). Either method is optional; omitted\n * methods fall back to the built-in defaults.\n */\n withIdentityProvider(provider: IdentityProvider): this {\n this._identityProvider = provider\n return this\n }\n\n withLogger(logger: MOSProxyLogger): this {\n this._logger = logger\n return this\n }\n\n /**\n * Register a callback to handle HTML pipeline errors. The proxy fails open by default and\n * returns the last safe (unconsumed) response — provide this if you want to shape that\n * response yourself (e.g. render a custom error page, return a 503, or re-throw so your\n * platform's error middleware handles it). If the callback throws or returns a non-Response\n * value, the proxy logs a warning and falls back to the last safe response.\n */\n withHtmlPipelineErrorHandler(handler: MOSProxyHtmlPipelineErrorHandler): this {\n this._onHtmlPipelineError = handler\n return this\n }\n\n withoutCustomEndpoints(): this {\n this._customEndpoints = false\n return this\n }\n\n withoutLinkRewriting(): this {\n this._linkRewriting = false\n return this\n }\n\n withoutSurfaceDecisions(): this {\n this._surfaceDecisions = false\n return this\n }\n\n /**\n * Disable the HTML transformation pipeline entirely (stages 3–6).\n *\n * Use for API-only proxies: neither an HTML rewriter adapter nor an API fetcher is required\n * if custom endpoints and surface decisions are both disabled alongside this.\n */\n withoutHtmlTransformation(): this {\n this._htmlTransformation = false\n return this\n }\n\n build(): MOSProxy {\n if (!this._config) {\n throw new Error('MOSProxyBuilder: withConfig(...) is required')\n }\n\n const needsHtmlRewriter = this._htmlTransformation && this._surfaceDecisions\n if (needsHtmlRewriter && !this._htmlRewriter) {\n throw new Error(\n 'MOSProxyBuilder: withHtmlRewriter(...) is required when surface decisions is enabled; call withoutSurfaceDecisions() or withoutHtmlTransformation() if you do not need it',\n )\n }\n\n const opts: MOSProxyOptions = {\n config: this._config,\n originFetcher: this._originFetcher,\n apiFetcher: this._apiFetcher,\n htmlRewriter: this._htmlRewriter,\n clientMetadataProvider: this._clientMetadataProvider,\n identityProvider: this._identityProvider,\n logger: this._logger ?? undefined,\n onHtmlPipelineError: this._onHtmlPipelineError ?? undefined,\n customEndpointsEnabled: this._customEndpoints,\n linkRewritingEnabled: this._linkRewriting,\n surfaceDecisionsEnabled: this._surfaceDecisions,\n htmlTransformationEnabled: this._htmlTransformation,\n }\n return new MOSProxy(opts)\n }\n}\n"]}
1
+ {"version":3,"file":"MOSProxyBuilder.js","sourceRoot":"","sources":["../src/MOSProxyBu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W;YAC5B,YAAY,EAAE,IAAI,CAAC,aAAa;YAChC,sBAAsB,EAAE,IAAI,CAAC,uBAAuB;YACpD,gBAAgB,EAAE,IAAI,CAAC,iBAAiB;YACxC,MAAM,EAAE,IAAI,CAAC,OAAO,IAAI,SAAS;YACjC,mBAAmB,EAAE,IAAI,CAAC,oBAAoB,IAAI,SAAS;YAC3D,sBAAsB,EAAE,IAAI,CAAC,gBAAgB;YAC7C,oBAAoB,EAAE,IAAI,CAAC,cAAc;YACzC,uBAAuB,EAAE,IAAI,CAAC,iBAAiB;YAC/C,yBAAyB,EAAE,IAAI,CAAC,mBAAmB;YACnD,yBAAyB,EAAE;gBACvB,GAAG,IAAI,CAAC,iCAAiC;gBACzC;oBACI,SAAS,EAAE,4BAA4B;oBACvC,MAAM,EAAE,MAAM;oBACd,OAAO,EAAE,2BAA2B;iBACvC;aACJ;SACJ,CAAA;QACD,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAA;IAC7B,CAAC;CACJ","sourcesContent":["import type { ClientMetadataProvider } from './adapters/ClientMetadataProvider'\nimport type { Fetcher } from './adapters/Fetcher'\nimport type { HtmlRewriterAdapter } from './adapters/HtmlRewriterAdapter'\nimport type { IdentityProvider } from './adapters/IdentityProvider'\nimport type { MOSProxyLogger } from './logger'\nimport { MOSProxy, type MOSProxyHtmlPipelineErrorHandler, type MOSProxyOptions } from './MOSProxy'\nimport type { MOSConfigInput, MosAuthenticatedApiRoute } from './types'\n\n/**\n * Fluent builder for `MOSProxy`. Provide configuration and platform adapters, then `build()`.\n *\n * Defaults:\n * - custom endpoint routing: enabled\n * - link rewriting: enabled\n * - surface decisions: enabled\n * - HTML transformation: enabled (auto-skipped per request for non-HTML responses)\n * - origin/API fetcher: `globalThis.fetch` (override on runtimes that need a backend binding, e.g. Fastly)\n *\n * @example\n * ```ts\n * const proxy = new MOSProxyBuilder()\n * .withConfig({\n * originUrl: 'https://news.example.com',\n * surfaceSlug: 'web',\n * mosHost: 'https://api.monetizationos.com',\n * mosSecretKey: process.env.MONETIZATION_OS_SECRET_KEY!,\n * mosEndpointsPrefix: '/mos-endpoints/',\n * anonymousSessionCookieName: 'anon-session-id',\n * authenticatedUserJwtCookieName: '__session',\n * injectScriptUrl: 'https://assets.monetizationos.com/web-components-latest.js',\n * surfaceDecisionsIgnorePaths: '',\n * originRequestHeaders: { 'X-Api-Key': process.env.ORIGIN_API_KEY! },\n * createAnonymousIdentifierFallback: true,\n * })\n * .withHtmlRewriter(myHtmlRewriterAdapter)\n * .build()\n *\n * export default { fetch: (request: Request) => proxy.handle(request) }\n * ```\n */\nexport class MOSProxyBuilder {\n private _config: MOSConfigInput | null = null\n private _originFetcher: Fetcher = globalThis.fetch\n private _apiFetcher: Fetcher = globalThis.fetch\n private _htmlRewriter: HtmlRewriterAdapter | null = null\n private _clientMetadataProvider: ClientMetadataProvider | null = null\n private _identityProvider: IdentityProvider | null = null\n private _logger: MOSProxyLogger | null = null\n private _onHtmlPipelineError: MOSProxyHtmlPipelineErrorHandler | null = null\n private _customEndpoints = true\n private _linkRewriting = true\n private _surfaceDecisions = true\n private _htmlTransformation = true\n private _additionalAuthenticatedApiRoutes: MosAuthenticatedApiRoute[] = []\n\n withConfig(config: MOSConfigInput): this {\n this._config = config\n return this\n }\n\n withOriginFetcher(fetcher: Fetcher): this {\n this._originFetcher = fetcher\n return this\n }\n\n withApiFetcher(fetcher: Fetcher): this {\n this._apiFetcher = fetcher\n return this\n }\n\n withHtmlRewriter(rewriter: HtmlRewriterAdapter): this {\n this._htmlRewriter = rewriter\n return this\n }\n\n withClientMetadata(provider: ClientMetadataProvider): this {\n this._clientMetadataProvider = provider\n return this\n }\n\n /**\n * Override identity provision for the surface-decisions API. Provide `resolve` to control the\n * identity payload sent to the API (e.g. resolved from a request header instead of cookies),\n * and/or `persist` to control how identity is recorded back on the response (e.g. suppress the\n * default anonymous-session cookie or write it elsewhere). Either method is optional; omitted\n * methods fall back to the built-in defaults.\n */\n withIdentityProvider(provider: IdentityProvider): this {\n this._identityProvider = provider\n return this\n }\n\n withLogger(logger: MOSProxyLogger): this {\n this._logger = logger\n return this\n }\n\n /**\n * Register a callback to handle HTML pipeline errors. The proxy fails open by default and\n * returns the last safe (unconsumed) response — provide this if you want to shape that\n * response yourself (e.g. render a custom error page, return a 503, or re-throw so your\n * platform's error middleware handles it). If the callback throws or returns a non-Response\n * value, the proxy logs a warning and falls back to the last safe response.\n */\n withHtmlPipelineErrorHandler(handler: MOSProxyHtmlPipelineErrorHandler): this {\n this._onHtmlPipelineError = handler\n return this\n }\n\n withoutCustomEndpoints(): this {\n this._customEndpoints = false\n return this\n }\n\n withoutLinkRewriting(): this {\n this._linkRewriting = false\n return this\n }\n\n withoutSurfaceDecisions(): this {\n this._surfaceDecisions = false\n return this\n }\n\n /**\n * Disable the HTML transformation pipeline entirely (stages 3–6).\n *\n * Use for API-only proxies: neither an HTML rewriter adapter nor an API fetcher is required\n * if custom endpoints and surface decisions are both disabled alongside this.\n */\n withoutHtmlTransformation(): this {\n this._htmlTransformation = false\n return this\n }\n\n /**\n * Add an additional route which, when matched by an incoming request, will be forwarded to the MonetizationOS API\n * with authentication handled by the configured identity provider.\n * This is in addition to the default `/mos-api/offer-redemptions` route.\n */\n withMosAuthenticatedApiRoutes(...routes: MosAuthenticatedApiRoute[]): this {\n this._additionalAuthenticatedApiRoutes.push(...routes)\n return this\n }\n\n build(): MOSProxy {\n if (!this._config) {\n throw new Error('MOSProxyBuilder: withConfig(...) is required')\n }\n\n const needsHtmlRewriter = this._htmlTransformation && this._surfaceDecisions\n if (needsHtmlRewriter && !this._htmlRewriter) {\n throw new Error(\n 'MOSProxyBuilder: withHtmlRewriter(...) is required when surface decisions is enabled; call withoutSurfaceDecisions() or withoutHtmlTransformation() if you do not need it',\n )\n }\n\n const opts: MOSProxyOptions = {\n config: this._config,\n originFetcher: this._originFetcher,\n apiFetcher: this._apiFetcher,\n htmlRewriter: this._htmlRewriter,\n clientMetadataProvider: this._clientMetadataProvider,\n identityProvider: this._identityProvider,\n logger: this._logger ?? undefined,\n onHtmlPipelineError: this._onHtmlPipelineError ?? undefined,\n customEndpointsEnabled: this._customEndpoints,\n linkRewritingEnabled: this._linkRewriting,\n surfaceDecisionsEnabled: this._surfaceDecisions,\n htmlTransformationEnabled: this._htmlTransformation,\n mosAuthenticatedApiRoutes: [\n ...this._additionalAuthenticatedApiRoutes,\n {\n matchPath: '/mos-api/offer-redemptions',\n method: 'POST',\n mosPath: '/api/v1/offer-redemptions',\n },\n ],\n }\n return new MOSProxy(opts)\n }\n}\n"]}
@@ -11,7 +11,7 @@ export type Identity = {
11
11
  };
12
12
  export interface ResolveIdentityArgs {
13
13
  request: Request;
14
- originResponse: Response;
14
+ originResponse?: Response;
15
15
  config: MOSConfig;
16
16
  logger: MOSProxyLogger;
17
17
  }
@@ -21,7 +21,7 @@ export const buildIdentity = ({ anonymousIdentifier, userJwt, createAnonymousIde
21
21
  return { createAnonymousIdentifier: true };
22
22
  };
23
23
  export const defaultResolveIdentity = (args) => buildIdentity({
24
- ...getExistingCookies(args.request, args.originResponse, args.config),
24
+ ...(args.originResponse ? getExistingCookies(args.request, args.originResponse, args.config) : {}),
25
25
  createAnonymousIdentifierFallback: args.config.createAnonymousIdentifierFallback,
26
26
  });
27
27
  export const defaultPersistIdentity = (args) => {
@@ -1 +1 @@
1
- {"version":3,"file":"IdentityProvider.js","sourceRoot":"","sources":["../../src/adapters/IdentityProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAA;AAmC9C,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAC9B,OAAgB,EAChB,cAAwB,EACxB,MAAiB,EACiC,EAAE;IACpD,MAAM,UAAU,GAAG,cAAc,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAA;IAChG,MAAM,qBAAqB,GAAG,UA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AA;AACN,CAAC,CAAA","sourcesContent":["import { parse, parseSetCookie } from 'cookie'\nimport type { MOSConfig } from '../config'\nimport type { MOSProxyLogger } from '../logger'\nimport type { SurfaceDecisionResponse } from '../types'\n\nexport type Identity =\n | { createAnonymousIdentifier: true }\n | { userJwt: string; createAnonymousIdentifierFallback?: true }\n | { anonymousIdentifier: string }\n\nexport interface ResolveIdentityArgs {\n request: Request\n originResponse: Response\n config: MOSConfig\n logger: MOSProxyLogger\n}\n\nexport interface PersistIdentityArgs {\n resolved: Identity\n decisions: SurfaceDecisionResponse\n response: Response\n request: Request\n config: MOSConfig\n logger: MOSProxyLogger\n}\n\n/**\n * Either method is optional; omitted methods use the built-in defaults. Both fail open: a throwing\n * `resolve` skips surface decisions; a throwing `persist` keeps the pre-persist response.\n */\nexport interface IdentityProvider {\n resolve?(args: ResolveIdentityArgs): Identity | Promise<Identity>\n persist?(args: PersistIdentityArgs): Response | Promise<Response>\n}\n\nexport const getExistingCookies = (\n request: Request,\n originResponse: Response,\n config: MOSConfig,\n): { anonymousIdentifier?: string; userJwt?: string } => {\n const setCookies = originResponse.headers.getSetCookie().map((header) => parseSetCookie(header))\n const originAnonymousCookie = setCookies.find((s) => s.name === config.anonymousSessionCookieName)\n const originUserJwtCookie = setCookies.find((s) => s.name === config.authenticatedUserJwtCookieName)\n if (originAnonymousCookie || originUserJwtCookie) {\n return { anonymousIdentifier: originAnonymousCookie?.value, userJwt: originUserJwtCookie?.value }\n }\n\n const cookies = parse(request.headers.get('Cookie') || '')\n const anonymousIdentifier = cookies[config.anonymousSessionCookieName]\n const userJwt = cookies[config.authenticatedUserJwtCookieName]\n return { anonymousIdentifier, userJwt }\n}\n\nexport const buildIdentity = ({\n anonymousIdentifier,\n userJwt,\n createAnonymousIdentifierFallback = false,\n}: {\n anonymousIdentifier?: string\n userJwt?: string\n createAnonymousIdentifierFallback?: boolean\n}): Identity => {\n if (userJwt) {\n return createAnonymousIdentifierFallback ? { userJwt, createAnonymousIdentifierFallback: true } : { userJwt }\n }\n\n if (anonymousIdentifier) {\n return { anonymousIdentifier }\n }\n\n return { createAnonymousIdentifier: true }\n}\n\nexport const defaultResolveIdentity = (args: ResolveIdentityArgs): Identity =>\n buildIdentity({\n ...getExistingCookies(args.request, args.originResponse, args.config),\n createAnonymousIdentifierFallback: args.config.createAnonymousIdentifierFallback,\n })\n\nexport const defaultPersistIdentity = (args: PersistIdentityArgs): Response => {\n const { resolved, decisions, response, request, config } = args\n const identifier = decisions.identity?.identifier\n if (!identifier) {\n return response\n }\n\n const minted = 'createAnonymousIdentifier' in resolved && resolved.createAnonymousIdentifier === true\n // A JWT resolved with the fallback flag mints a fresh anonymous identifier only when the API reports the\n // JWT as unauthenticated, and only when the request did not already carry an anonymous session cookie.\n const jwtFallback =\n 'createAnonymousIdentifierFallback' in resolved &&\n resolved.createAnonymousIdentifierFallback === true &&\n !decisions.identity?.isAuthenticated &&\n !getExistingCookies(request, response, config).anonymousIdentifier\n\n if (!minted && !jwtFallback) {\n return response\n }\n\n const headers = new Headers(response.headers)\n headers.append('Set-Cookie', `${config.anonymousSessionCookieName}=${identifier}; Path=/`)\n return new Response(response.body, {\n status: response.status,\n statusText: response.statusText,\n headers,\n })\n}\n"]}
1
+ {"version":3,"file":"IdentityProvider.js","sourceRoot":"","sources":["../../src/adapters/IdentityProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAA;AAmC9C,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAC9B,OAAgB,EAChB,cAAwB,EACxB,MAAiB,EACiC,EAAE;IACpD,MAAM,UAAU,GAAG,cAAc,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAA;IAChG,MAAM,qBAAqB,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC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,CAAC,CAAA;AACN,CAAC,CAAA","sourcesContent":["import { parse, parseSetCookie } from 'cookie'\nimport type { MOSConfig } from '../config'\nimport type { MOSProxyLogger } from '../logger'\nimport type { SurfaceDecisionResponse } from '../types'\n\nexport type Identity =\n | { createAnonymousIdentifier: true }\n | { userJwt: string; createAnonymousIdentifierFallback?: true }\n | { anonymousIdentifier: string }\n\nexport interface ResolveIdentityArgs {\n request: Request\n originResponse?: Response\n config: MOSConfig\n logger: MOSProxyLogger\n}\n\nexport interface PersistIdentityArgs {\n resolved: Identity\n decisions: SurfaceDecisionResponse\n response: Response\n request: Request\n config: MOSConfig\n logger: MOSProxyLogger\n}\n\n/**\n * Either method is optional; omitted methods use the built-in defaults. Both fail open: a throwing\n * `resolve` skips surface decisions; a throwing `persist` keeps the pre-persist response.\n */\nexport interface IdentityProvider {\n resolve?(args: ResolveIdentityArgs): Identity | Promise<Identity>\n persist?(args: PersistIdentityArgs): Response | Promise<Response>\n}\n\nexport const getExistingCookies = (\n request: Request,\n originResponse: Response,\n config: MOSConfig,\n): { anonymousIdentifier?: string; userJwt?: string } => {\n const setCookies = originResponse.headers.getSetCookie().map((header) => parseSetCookie(header))\n const originAnonymousCookie = setCookies.find((s) => s.name === config.anonymousSessionCookieName)\n const originUserJwtCookie = setCookies.find((s) => s.name === config.authenticatedUserJwtCookieName)\n if (originAnonymousCookie || originUserJwtCookie) {\n return { anonymousIdentifier: originAnonymousCookie?.value, userJwt: originUserJwtCookie?.value }\n }\n\n const cookies = parse(request.headers.get('Cookie') || '')\n const anonymousIdentifier = cookies[config.anonymousSessionCookieName]\n const userJwt = cookies[config.authenticatedUserJwtCookieName]\n return { anonymousIdentifier, userJwt }\n}\n\nexport const buildIdentity = ({\n anonymousIdentifier,\n userJwt,\n createAnonymousIdentifierFallback = false,\n}: {\n anonymousIdentifier?: string\n userJwt?: string\n createAnonymousIdentifierFallback?: boolean\n}): Identity => {\n if (userJwt) {\n return createAnonymousIdentifierFallback ? { userJwt, createAnonymousIdentifierFallback: true } : { userJwt }\n }\n\n if (anonymousIdentifier) {\n return { anonymousIdentifier }\n }\n\n return { createAnonymousIdentifier: true }\n}\n\nexport const defaultResolveIdentity = (args: ResolveIdentityArgs): Identity =>\n buildIdentity({\n ...(args.originResponse ? getExistingCookies(args.request, args.originResponse, args.config) : {}),\n createAnonymousIdentifierFallback: args.config.createAnonymousIdentifierFallback,\n })\n\nexport const defaultPersistIdentity = (args: PersistIdentityArgs): Response => {\n const { resolved, decisions, response, request, config } = args\n const identifier = decisions.identity?.identifier\n if (!identifier) {\n return response\n }\n\n const minted = 'createAnonymousIdentifier' in resolved && resolved.createAnonymousIdentifier === true\n // A JWT resolved with the fallback flag mints a fresh anonymous identifier only when the API reports the\n // JWT as unauthenticated, and only when the request did not already carry an anonymous session cookie.\n const jwtFallback =\n 'createAnonymousIdentifierFallback' in resolved &&\n resolved.createAnonymousIdentifierFallback === true &&\n !decisions.identity?.isAuthenticated &&\n !getExistingCookies(request, response, config).anonymousIdentifier\n\n if (!minted && !jwtFallback) {\n return response\n }\n\n const headers = new Headers(response.headers)\n headers.append('Set-Cookie', `${config.anonymousSessionCookieName}=${identifier}; Path=/`)\n return new Response(response.body, {\n status: response.status,\n statusText: response.statusText,\n headers,\n })\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  export declare const MOS_PROXY_VERSION_HEADER = "X-MOS-Proxy-Version";
2
2
  export declare const MOS_PROXY_CLIENT_HEADER = "X-MOS-Proxy-Client";
3
- export declare const MOS_PROXY_PACKAGE_VERSION = "1.2.0";
3
+ export declare const MOS_PROXY_PACKAGE_VERSION = "1.3.0";
4
4
  export declare const setMosProxyHeaders: (headers: Headers) => void;
5
5
  export declare const withMosProxyHeaders: (headers?: HeadersInit) => Headers;
@@ -1,7 +1,7 @@
1
1
  import { formatClientRuntimeHeader } from './clientRuntime';
2
2
  export const MOS_PROXY_VERSION_HEADER = 'X-MOS-Proxy-Version';
3
3
  export const MOS_PROXY_CLIENT_HEADER = 'X-MOS-Proxy-Client';
4
- export const MOS_PROXY_PACKAGE_VERSION = '1.2.0';
4
+ export const MOS_PROXY_PACKAGE_VERSION = '1.3.0';
5
5
  export const setMosProxyHeaders = (headers) => {
6
6
  headers.set(MOS_PROXY_VERSION_HEADER, MOS_PROXY_PACKAGE_VERSION);
7
7
  headers.set(MOS_PROXY_CLIENT_HEADER, formatClientRuntimeHeader());
@@ -1 +1 @@
1
- {"version":3,"file":"apiRequestHeaders.js","sourceRoot":"","sources":["../src/apiRequestHeaders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAA;AAE3D,MAAM,CAAC,MAAM,wBAAwB,GAAG,qBAAqB,CAAA;AAC7D,MAAM,CAAC,MAAM,uBAAuB,GAAG,oBAAoB,CAAA;AAC3D,MAAM,CAAC,MAAM,yBAAyB,GAAG,OAAO,CAAA;AAEhD,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,OAAgB,EAAQ,EAAE;IACzD,OAAO,CAAC,GAAG,CAAC,wBAAwB,EAAE,yBAAyB,CAAC,CAAA;IAChE,OAAO,CAAC,GAAG,CAAC,uBAAuB,EAAE,yBAAyB,EAAE,CAAC,CAAA;AACrE,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,OAAqB,EAAW,EAAE;IAClE,MAAM,IAAI,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;IACjC,kBAAkB,CAAC,IAAI,CAAC,CAAA;IACxB,OAAO,IAAI,CAAA;AACf,CAAC,CAAA","sourcesContent":["import { formatClientRuntimeHeader } from './clientRuntime'\n\nexport const MOS_PROXY_VERSION_HEADER = 'X-MOS-Proxy-Version'\nexport const MOS_PROXY_CLIENT_HEADER = 'X-MOS-Proxy-Client'\nexport const MOS_PROXY_PACKAGE_VERSION = '1.2.0'\n\nexport const setMosProxyHeaders = (headers: Headers): void => {\n headers.set(MOS_PROXY_VERSION_HEADER, MOS_PROXY_PACKAGE_VERSION)\n headers.set(MOS_PROXY_CLIENT_HEADER, formatClientRuntimeHeader())\n}\n\nexport const withMosProxyHeaders = (headers?: HeadersInit): Headers => {\n const next = new Headers(headers)\n setMosProxyHeaders(next)\n return next\n}\n"]}
1
+ {"version":3,"file":"apiRequestHeaders.js","sourceRoot":"","sources":["../src/apiRequestHeaders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAA;AAE3D,MAAM,CAAC,MAAM,wBAAwB,GAAG,qBAAqB,CAAA;AAC7D,MAAM,CAAC,MAAM,uBAAuB,GAAG,oBAAoB,CAAA;AAC3D,MAAM,CAAC,MAAM,yBAAyB,GAAG,OAAO,CAAA;AAEhD,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,OAAgB,EAAQ,EAAE;IACzD,OAAO,CAAC,GAAG,CAAC,wBAAwB,EAAE,yBAAyB,CAAC,CAAA;IAChE,OAAO,CAAC,GAAG,CAAC,uBAAuB,EAAE,yBAAyB,EAAE,CAAC,CAAA;AACrE,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,OAAqB,EAAW,EAAE;IAClE,MAAM,IAAI,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;IACjC,kBAAkB,CAAC,IAAI,CAAC,CAAA;IACxB,OAAO,IAAI,CAAA;AACf,CAAC,CAAA","sourcesContent":["import { formatClientRuntimeHeader } from './clientRuntime'\n\nexport const MOS_PROXY_VERSION_HEADER = 'X-MOS-Proxy-Version'\nexport const MOS_PROXY_CLIENT_HEADER = 'X-MOS-Proxy-Client'\nexport const MOS_PROXY_PACKAGE_VERSION = '1.3.0'\n\nexport const setMosProxyHeaders = (headers: Headers): void => {\n headers.set(MOS_PROXY_VERSION_HEADER, MOS_PROXY_PACKAGE_VERSION)\n headers.set(MOS_PROXY_CLIENT_HEADER, formatClientRuntimeHeader())\n}\n\nexport const withMosProxyHeaders = (headers?: HeadersInit): Headers => {\n const next = new Headers(headers)\n setMosProxyHeaders(next)\n return next\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import { type ClientMetadataProvider, type Fetcher, type IdentityProvider } from '../adapters';
2
+ import type { PipelineContext } from '../context';
3
+ import type { MosAuthenticatedApiRoute } from '../types';
4
+ export declare const handleMosAuthenticatedApiRoutes: (request: Request, routes: MosAuthenticatedApiRoute[], ctx: PipelineContext, identityProvider: IdentityProvider | null, clientMetadataProvider: ClientMetadataProvider | null, apiFetcher: Fetcher | null) => Promise<Response | null>;
@@ -0,0 +1,36 @@
1
+ import { defaultResolveIdentity } from '../adapters';
2
+ import { withMosProxyHeaders } from '../apiRequestHeaders';
3
+ export const handleMosAuthenticatedApiRoutes = async (request, routes, ctx, identityProvider, clientMetadataProvider, apiFetcher) => {
4
+ if (!routes?.length) {
5
+ return null;
6
+ }
7
+ const url = new URL(request.url);
8
+ for (const route of routes) {
9
+ if (url.pathname === route.matchPath && request.method.toUpperCase() === route.method) {
10
+ return handleMosAuthenticatedApiRoute(request, route, ctx, identityProvider, clientMetadataProvider, apiFetcher);
11
+ }
12
+ }
13
+ return null;
14
+ };
15
+ const handleMosAuthenticatedApiRoute = async (request, route, ctx, identityProvider, clientMetadataProvider, apiFetcher) => {
16
+ if (!apiFetcher) {
17
+ throw new Error('MOSProxy: mosAuthenticatedApiRoutes are configured but no API fetcher is configured');
18
+ }
19
+ const { config, logger } = ctx;
20
+ const requestBody = await request.json();
21
+ const identity = await (identityProvider?.resolve ?? defaultResolveIdentity)({ request, config, logger });
22
+ return await apiFetcher(new Request(new URL(route.mosPath, config.mosHost), {
23
+ method: route.method,
24
+ body: JSON.stringify({
25
+ ...requestBody,
26
+ ...(clientMetadataProvider?.build(request) ?? {}),
27
+ identity,
28
+ http: { url: request.url, userAgent: request.headers.get('User-Agent') ?? undefined },
29
+ }),
30
+ headers: withMosProxyHeaders({
31
+ 'Content-Type': 'application/json',
32
+ Authorization: `Bearer ${config.mosSecretKey}`,
33
+ }),
34
+ }));
35
+ };
36
+ //# sourceMappingURL=mosAuthenticatedApi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mosAuthenticatedApi.js","sourceRoot":"","sources":["../../src/stages/mosAuthenticatedApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,sBAAsB,EAAuC,MAAM,aAAa,CAAA;AACtH,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAA;AAI1D,MAAM,CAAC,MAAM,+BAA+B,GAAG,KAAK,EAChD,OAAgB,EAChB,MAAkC,EAClC,GAAoB,EACpB,gBAAyC,EACzC,sBAAqD,EACrD,UAA0B,EACF,EAAE;IAC1B,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;QAClB,OAAO,IAAI,CAAA;IACf,CAAC;IAED,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IAChC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QACzB,IAAI,GAAG,CAAC,QAAQ,KAAK,KAAK,CAAC,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC;YACpF,OAAO,8BAA8B,CAAC,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,UAAU,CAAC,CAAA;QACpH,CAAC;IACL,CAAC;IAED,OAAO,IAAI,CAAA;AACf,CAAC,CAAA;AAED,MAAM,8BAA8B,GAAG,KAAK,EACxC,OAAgB,EAChB,KAA+B,EAC/B,GAAoB,EACpB,gBAAyC,EACzC,sBAAqD,EACrD,UAA0B,EACF,EAAE;IAC1B,IAAI,CAAC,UAAU,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,qFAAqF,CAAC,CAAA;IAC1G,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAA;IAC9B,MAAM,WAAW,GAAG,MAAM,OAAO,CAAC,IAAI,EAAE,CAAA;IACxC,MAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,EAAE,OAAO,IAAI,sBAAsB,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;IAEzG,OAAO,MAAM,UAAU,CACnB,IAAI,OAAO,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE;QAChD,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;YACjB,GAAG,WAAW;YACd,GAAG,CAAC,sBAAsB,EAAE,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;YACjD,QAAQ;YACR,IAAI,EAAE,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,SAAS,EAAE;SACxF,CAAC;QACF,OAAO,EAAE,mBAAmB,CAAC;YACzB,cAAc,EAAE,kBAAkB;YAClC,aAAa,EAAE,UAAU,MAAM,CAAC,YAAY,EAAE;SACjD,CAAC;KACL,CAAC,CACL,CAAA;AACL,CAAC,CAAA","sourcesContent":["import { type ClientMetadataProvider, defaultResolveIdentity, type Fetcher, type IdentityProvider } from '../adapters'\nimport { withMosProxyHeaders } from '../apiRequestHeaders'\nimport type { PipelineContext } from '../context'\nimport type { MosAuthenticatedApiRoute } from '../types'\n\nexport const handleMosAuthenticatedApiRoutes = async (\n request: Request,\n routes: MosAuthenticatedApiRoute[],\n ctx: PipelineContext,\n identityProvider: IdentityProvider | null,\n clientMetadataProvider: ClientMetadataProvider | null,\n apiFetcher: Fetcher | null,\n): Promise<Response | null> => {\n if (!routes?.length) {\n return null\n }\n\n const url = new URL(request.url)\n for (const route of routes) {\n if (url.pathname === route.matchPath && request.method.toUpperCase() === route.method) {\n return handleMosAuthenticatedApiRoute(request, route, ctx, identityProvider, clientMetadataProvider, apiFetcher)\n }\n }\n\n return null\n}\n\nconst handleMosAuthenticatedApiRoute = async (\n request: Request,\n route: MosAuthenticatedApiRoute,\n ctx: PipelineContext,\n identityProvider: IdentityProvider | null,\n clientMetadataProvider: ClientMetadataProvider | null,\n apiFetcher: Fetcher | null,\n): Promise<Response | null> => {\n if (!apiFetcher) {\n throw new Error('MOSProxy: mosAuthenticatedApiRoutes are configured but no API fetcher is configured')\n }\n\n const { config, logger } = ctx\n const requestBody = await request.json()\n const identity = await (identityProvider?.resolve ?? defaultResolveIdentity)({ request, config, logger })\n\n return await apiFetcher(\n new Request(new URL(route.mosPath, config.mosHost), {\n method: route.method,\n body: JSON.stringify({\n ...requestBody,\n ...(clientMetadataProvider?.build(request) ?? {}),\n identity,\n http: { url: request.url, userAgent: request.headers.get('User-Agent') ?? undefined },\n }),\n headers: withMosProxyHeaders({\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${config.mosSecretKey}`,\n }),\n }),\n )\n}\n"]}
package/dist/types.d.ts CHANGED
@@ -92,6 +92,11 @@ export interface SurfaceDecisionError {
92
92
  status: 'error';
93
93
  statusCode: number;
94
94
  }
95
+ export type MosAuthenticatedApiRoute = {
96
+ matchPath: string;
97
+ method: 'POST' | 'PUT' | 'PATCH';
98
+ mosPath: string;
99
+ };
95
100
  export interface MOSConfigInput {
96
101
  /** Origin website base URL, e.g. `https://news.example.com`. */
97
102
  originUrl: string;
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export type PageMetadata = Record<string, string>\n\nexport interface FeatureMeterableProperty {\n type: 'meterable'\n hasAccess: boolean\n remainingUnits?: number\n}\n\nexport interface FeatureNumberProperty {\n type: 'number'\n value: number\n}\n\nexport interface Feature {\n featureSlug: string\n properties: Record<string, FeatureMeterableProperty | FeatureNumberProperty>\n sideEffects: unknown[]\n}\n\nexport interface SubSurfaceMetadataApi {\n cssSelector?: string | null\n}\n\nexport type SetHttpResponse = {\n headers?: Record<string, string>\n cookies?: string[]\n status: number\n statusText?: string\n body: string | null\n}\n\nexport type ModifyHttpResponse = {\n addHeaders?: { name: string; value: string }[]\n removeHeaders?: string[]\n addCookies?: string[]\n status?: number\n statusText?: string\n body?: string | null\n}\n\nexport type SurfaceBehaviorApi = {\n http?: ModifyHttpResponse | SetHttpResponse\n properties?: Record<string, unknown>\n} & Record<string, unknown>\n\nexport type WebComponentElement<T = Record<string, unknown>> = {\n schema: string\n props: T\n}\n\nexport type WebElement =\n | {\n type: 'html'\n content: string\n }\n | {\n type: 'text'\n content: string\n }\n | ({\n type: 'element'\n } & WebComponentElement)\n | ({\n type: 'custom'\n } & Record<string, unknown>)\n\nexport type WebComponentRangeReplacement = {\n fromMarker?: string\n toMarker?: string\n replaceWith?: WebElement[] | null\n}\n\nexport type WebContentSurfaceBehavior = {\n before?: WebElement[]\n prepend?: WebElement[]\n remove?: boolean\n replaceRange?: WebComponentRangeReplacement | null\n append?: WebElement[]\n after?: WebElement[]\n}\n\nexport type SubSurfaceBehaviorApi = {\n content?: WebContentSurfaceBehavior\n properties?: Record<string, unknown>\n metadata: SubSurfaceMetadataApi\n} & Record<string, unknown>\n\nexport interface SurfaceDecisionResponse {\n status: 'success'\n identity: {\n identifier: string\n isAuthenticated: boolean\n authType: string\n jwtClaims: Record<string, unknown>\n }\n features: Record<string, Feature>\n customer: {\n hasProducts: boolean\n }\n surfaceBehavior: SurfaceBehaviorApi\n componentsSkipped: boolean\n componentBehaviors: Record<string, SubSurfaceBehaviorApi>\n}\n\nexport interface SurfaceDecisionError {\n message: string\n status: 'error'\n statusCode: number\n}\n\nexport interface MOSConfigInput {\n /** Origin website base URL, e.g. `https://news.example.com`. */\n originUrl: string\n /** Surface slug identifying the MonetizationOS surface. */\n surfaceSlug: string\n /** MonetizationOS API base URL. */\n mosHost: string\n /** MonetizationOS secret key; expected format `sk_<env>_<suffix>` */\n mosSecretKey: string\n /** Path prefix routed to the MonetizationOS endpoint proxy. Default: `/mos-endpoints/`. */\n mosEndpointsPrefix?: string\n /** Cookie name storing the anonymous session id. */\n anonymousSessionCookieName: string\n /** Cookie name storing the authenticated user JWT. */\n authenticatedUserJwtCookieName: string\n /** Optional script URL injected into the `<head>` of HTML responses. */\n injectScriptUrl?: string\n /** Comma-separated regex patterns; matching pathnames skip surface decisions. */\n surfaceDecisionsIgnorePaths?: string\n /** Optional headers added to or replacing client headers on every origin request. */\n originRequestHeaders?: Record<string, string>\n /**\n * When true (default), JWT surface-decision requests include `createAnonymousIdentifierFallback: true`\n * so MonetizationOS can mint an anonymous identifier if JWT authentication fails.\n * Set to `false` to opt out.\n */\n createAnonymousIdentifierFallback?: boolean\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export type PageMetadata = Record<string, string>\n\nexport interface FeatureMeterableProperty {\n type: 'meterable'\n hasAccess: boolean\n remainingUnits?: number\n}\n\nexport interface FeatureNumberProperty {\n type: 'number'\n value: number\n}\n\nexport interface Feature {\n featureSlug: string\n properties: Record<string, FeatureMeterableProperty | FeatureNumberProperty>\n sideEffects: unknown[]\n}\n\nexport interface SubSurfaceMetadataApi {\n cssSelector?: string | null\n}\n\nexport type SetHttpResponse = {\n headers?: Record<string, string>\n cookies?: string[]\n status: number\n statusText?: string\n body: string | null\n}\n\nexport type ModifyHttpResponse = {\n addHeaders?: { name: string; value: string }[]\n removeHeaders?: string[]\n addCookies?: string[]\n status?: number\n statusText?: string\n body?: string | null\n}\n\nexport type SurfaceBehaviorApi = {\n http?: ModifyHttpResponse | SetHttpResponse\n properties?: Record<string, unknown>\n} & Record<string, unknown>\n\nexport type WebComponentElement<T = Record<string, unknown>> = {\n schema: string\n props: T\n}\n\nexport type WebElement =\n | {\n type: 'html'\n content: string\n }\n | {\n type: 'text'\n content: string\n }\n | ({\n type: 'element'\n } & WebComponentElement)\n | ({\n type: 'custom'\n } & Record<string, unknown>)\n\nexport type WebComponentRangeReplacement = {\n fromMarker?: string\n toMarker?: string\n replaceWith?: WebElement[] | null\n}\n\nexport type WebContentSurfaceBehavior = {\n before?: WebElement[]\n prepend?: WebElement[]\n remove?: boolean\n replaceRange?: WebComponentRangeReplacement | null\n append?: WebElement[]\n after?: WebElement[]\n}\n\nexport type SubSurfaceBehaviorApi = {\n content?: WebContentSurfaceBehavior\n properties?: Record<string, unknown>\n metadata: SubSurfaceMetadataApi\n} & Record<string, unknown>\n\nexport interface SurfaceDecisionResponse {\n status: 'success'\n identity: {\n identifier: string\n isAuthenticated: boolean\n authType: string\n jwtClaims: Record<string, unknown>\n }\n features: Record<string, Feature>\n customer: {\n hasProducts: boolean\n }\n surfaceBehavior: SurfaceBehaviorApi\n componentsSkipped: boolean\n componentBehaviors: Record<string, SubSurfaceBehaviorApi>\n}\n\nexport interface SurfaceDecisionError {\n message: string\n status: 'error'\n statusCode: number\n}\n\nexport type MosAuthenticatedApiRoute = {\n matchPath: string\n method: 'POST' | 'PUT' | 'PATCH'\n mosPath: string\n}\n\nexport interface MOSConfigInput {\n /** Origin website base URL, e.g. `https://news.example.com`. */\n originUrl: string\n /** Surface slug identifying the MonetizationOS surface. */\n surfaceSlug: string\n /** MonetizationOS API base URL. */\n mosHost: string\n /** MonetizationOS secret key; expected format `sk_<env>_<suffix>` */\n mosSecretKey: string\n /** Path prefix routed to the MonetizationOS endpoint proxy. Default: `/mos-endpoints/`. */\n mosEndpointsPrefix?: string\n /** Cookie name storing the anonymous session id. */\n anonymousSessionCookieName: string\n /** Cookie name storing the authenticated user JWT. */\n authenticatedUserJwtCookieName: string\n /** Optional script URL injected into the `<head>` of HTML responses. */\n injectScriptUrl?: string\n /** Comma-separated regex patterns; matching pathnames skip surface decisions. */\n surfaceDecisionsIgnorePaths?: string\n /** Optional headers added to or replacing client headers on every origin request. */\n originRequestHeaders?: Record<string, string>\n /**\n * When true (default), JWT surface-decision requests include `createAnonymousIdentifierFallback: true`\n * so MonetizationOS can mint an anonymous identifier if JWT authentication fails.\n * Set to `false` to opt out.\n */\n createAnonymousIdentifierFallback?: boolean\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monetizationos/proxy",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Platform-agnostic MonetizationOS proxy core",
5
5
  "license": "MIT",
6
6
  "type": "module",