@closerepo/notification-contracts 1.12.0 → 1.14.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/dist/render.js ADDED
@@ -0,0 +1,24 @@
1
+ import{z as e}from"zod";e.strictObject({eventId:e.uuid(),deliverBy:e.iso.datetime(),locale:e.string().min(1).max(35),contentLocale:e.string().min(1).max(35),title:e.string().trim().min(1).max(512),body:e.string(),url:e.url().refine(e=>new URL(e).protocol===`https:`,`notification_https_target_required`)});const t=32*1024,n=1380*60*1e3,r=t,i=t,a=n,o=32*1024,s=[`pending`,`delivering`,`partial`,`delivered`,`failed`,`expired`],c=[`accepted`,`enqueue_retry_wait`,`in_progress`,`partial_delivery`,`complete`,`retry_exhausted`,`delivery_deadline_exceeded`,`destination_configuration_unavailable`],l=[...s,`unconfirmed`],u=[...c,`delivery_unconfirmed`],d=[`pending`,`delivering`,`delivered`,`failed`,`expired`],f=[...d,`unconfirmed`],p=[`provider_timeout`,`provider_rate_limited`,`provider_unavailable`,`provider_rejected`,`destination_configuration_unavailable`,`delivery_interrupted`,`retry_exhausted`],m=[`invalid_request`,`request_too_large`,`producer_scope_mismatch`,`route_not_allowed`,`event_identity_conflict`,`route_not_found`,`route_empty`,`delivery_deadline_invalid`],h=[`invalid_type`,`invalid_value`,`too_small`,`too_big`,`invalid_format`,`unrecognized_keys`,`custom`],g=[`qq`,`tg`,`wechat`,`lark`],ee=new TextEncoder,_=/^[a-z][a-z0-9-]{0,62}$/,te=/^[a-z][a-z0-9-]{0,31}(?:\.[a-z][a-z0-9-]{0,31}){1,3}$/,ne=/^[A-Z0-9][\w.:-]{0,127}$/i;function v(e){return[...e].length}function y(t){return e.string().refine(e=>v(e)>0,{message:`too_small`}).refine(e=>v(e)<=t,{message:`too_big`})}const b=e.string().datetime({offset:!0}),x=e.object({key:e.string().regex(_),environment:e.string().regex(_)}).strict(),S=e.object({type:e.string().regex(_),id:e.string().regex(ne),revision:e.number().int().positive()}).strict(),C=e.string().min(1).max(127).regex(te),w=e.object({label:y(48),url:y(2048).refine(e=>[...e].every(e=>{let t=e.codePointAt(0);return t!==void 0&&t>31&&t!==127}),{message:`invalid_https_url`}).refine(e=>{try{return new URL(e).protocol===`https:`}catch{return!1}},{message:`invalid_https_url`})}).strict(),T={eventType:e.literal(`notification.delivery.requested`),eventId:e.string().uuid(),producer:x,routeKey:C,occurredAt:b,requestedAt:b,deliverBy:b,subject:S},re=e.object({contractVersion:e.literal(1),...T,message:e.object({type:e.literal(`link_summary`),title:y(160),summary:y(1200),facts:e.array(e.object({label:y(48),value:y(500)}).strict()).max(10),primaryLink:w,sourceLink:w.optional()}).strict()}).strict();function E(e,t,n,r){let i=Date.parse(e.requestedAt),a=Date.parse(e.deliverBy);(!Number.isFinite(i)||!Number.isFinite(a)||a<=i||a-i>n)&&t.addIssue({code:`custom`,message:`delivery_deadline_invalid`,path:[`deliverBy`]}),ee.encode(JSON.stringify(e)).byteLength>r&&t.addIssue({code:`custom`,message:`request_too_large`})}const ie=re.superRefine((e,r)=>E(e,r,n,t)),ae={title:y(160),summary:y(1200),facts:e.array(e.object({label:y(48),value:y(500)}).strict()).max(10)},oe=e.discriminatedUnion(`key`,[e.object({key:e.literal(`source`),locale:e.literal(`und`),...ae}).strict(),e.object({key:e.literal(`zh-CN`),locale:e.literal(`zh-CN`),...ae}).strict()]),se=e.array(oe).min(1).max(2).superRefine((e,t)=>{e.filter(e=>e.key===`source`).length!==1&&t.addIssue({code:`custom`,message:`source_variant_required_once`}),new Set(e.map(e=>e.key)).size!==e.length&&t.addIssue({code:`custom`,message:`message_variant_duplicate`})}),ce=e.object({contractVersion:e.literal(2),...T,message:e.object({type:e.literal(`localized_link_summary`),variants:se,primaryLink:w,sourceLink:w.optional()}).strict()}).strict().superRefine((e,t)=>E(e,t,828e5,32768)),D=e.discriminatedUnion(`type`,[e.object({key:e.string().regex(_),type:e.literal(`text`),label:y(48).optional(),value:y(4e3)}).strict(),e.object({key:e.string().regex(_),type:e.literal(`fields`),items:e.array(e.object({label:y(48),value:y(1200)}).strict()).min(1).max(20)}).strict()]),O=e.array(D).max(30).superRefine((e,t)=>{new Set(e.map(e=>e.key)).size!==e.length&&t.addIssue({code:`custom`,message:`message_section_duplicate`})}),le=O.min(1),k=e.discriminatedUnion(`key`,[e.object({key:e.literal(`source`),locale:e.literal(`und`),title:y(160),sections:le}).strict(),e.object({key:e.literal(`zh-CN`),locale:e.literal(`zh-CN`),title:y(160),sections:le}).strict()]),A=(e,t,n=!1)=>{e.filter(e=>e.key===`source`).length!==1&&t.addIssue({code:`custom`,message:`source_variant_required_once`}),new Set(e.map(e=>e.key)).size!==e.length&&t.addIssue({code:`custom`,message:`message_variant_duplicate`});let r=e.find(e=>e.key===`source`),i=e.find(e=>e.key===`zh-CN`);if(r&&i){let a=new Set(r.sections.map(e=>e.key)),o=new Set(i.sections.map(e=>e.key)),s=r.sections.filter(e=>!n||e.type!==`text`||o.has(e.key)).map(e=>`${e.key}:${e.type}`),c=i.sections.filter(e=>!n||e.type!==`text`||a.has(e.key)).map(e=>`${e.key}:${e.type}`);JSON.stringify(s)!==JSON.stringify(c)&&t.addIssue({code:`custom`,message:`message_variant_structure_mismatch`,path:[e.indexOf(i),`sections`]})}},ue=e.array(k).min(1).max(2).superRefine(A),j=e.object({contractVersion:e.literal(3),...T,message:e.object({type:e.literal(`structured_notification`),variants:ue,links:e.array(e.object({key:e.string().regex(_),...w.shape}).strict()).max(10).superRefine((e,t)=>{new Set(e.map(e=>e.key)).size!==e.length&&t.addIssue({code:`custom`,message:`message_link_duplicate`})})}).strict()}).strict(),de=j.superRefine((e,t)=>E(e,t,828e5,32768)),M=e.object({label:y(48).optional(),value:y(r)}).strict(),fe=e.discriminatedUnion(`key`,[k.options[0].extend({body:M}),k.options[1].extend({body:M})]),pe=j.extend({contractVersion:e.literal(4),message:j.shape.message.extend({variants:e.array(fe).min(1).max(2).superRefine(A)})}).superRefine((e,t)=>E(e,t,828e5,r)),N=e.discriminatedUnion(`key`,[k.options[0].extend({bodyLabel:y(48).optional(),linkLabels:e.array(e.object({key:e.string().regex(_),label:y(48)}).strict()).max(10).superRefine((e,t)=>{new Set(e.map(e=>e.key)).size!==e.length&&t.addIssue({code:`custom`,message:`message_link_label_duplicate`})})}),k.options[1].extend({bodyLabel:y(48).optional(),linkLabels:e.array(e.object({key:e.string().regex(_),label:y(48)}).strict()).max(10).superRefine((e,t)=>{new Set(e.map(e=>e.key)).size!==e.length&&t.addIssue({code:`custom`,message:`message_link_label_duplicate`})})})]),me=e.object({value:y(i)}).strict(),P=e.object({contractVersion:e.literal(5),...T,message:e.object({type:e.literal(`structured_notification`),variants:e.array(N).min(1).max(2).superRefine(A),links:e.array(e.object({key:e.string().regex(_),url:w.shape.url}).strict()).max(10).superRefine((e,t)=>{new Set(e.map(e=>e.key)).size!==e.length&&t.addIssue({code:`custom`,message:`message_link_duplicate`})}),body:me}).strict()}).strict().superRefine((e,t)=>{E(e,t,a,i);let n=e.message.links.map(e=>e.key);for(let[r,i]of e.message.variants.entries()){let e=i.linkLabels.map(e=>e.key);JSON.stringify(e)!==JSON.stringify(n)&&t.addIssue({code:`custom`,message:`message_variant_link_labels_mismatch`,path:[`message`,`variants`,r,`linkLabels`]})}}),F=e.object({...P.shape,contractVersion:e.literal(6),message:P.shape.message.omit({body:!0}).extend({bodies:e.array(e.object({key:e.string().regex(_),value:y(i)}).strict()).max(2),variants:e.array(e.discriminatedUnion(`key`,[N.options[0].omit({bodyLabel:!0}).extend({sections:O,bodyRefs:e.array(e.object({key:e.string().regex(_),label:y(48).optional()}).strict()).max(2),footer:y(500).optional()}),N.options[1].omit({bodyLabel:!0}).extend({sections:O,bodyRefs:e.array(e.object({key:e.string().regex(_),label:y(48).optional()}).strict()).max(2),footer:y(500).optional()})])).min(1).max(2).superRefine((e,t)=>A(e,t,!0))})}).strict().superRefine(I);function I(e,t){E(e,t,a,i);let n=e.message.bodies.map(e=>e.key),r=e.message.links.map(e=>e.key);new Set(n).size!==n.length&&t.addIssue({code:`custom`,message:`message_body_duplicate`}),he(e.message.variants,n,r,t)}function he(e,t,n,r,i){for(let[a,o]of e.entries()){let e=o.bodyRefs.map(e=>e.key);(new Set(e).size!==e.length||e.some(e=>!t.includes(e)))&&r.addIssue({code:`custom`,message:`message_body_reference_invalid`,...i?{path:[...i,a,`bodyRefs`]}:{}}),JSON.stringify(o.linkLabels.map(e=>e.key))!==JSON.stringify(n)&&r.addIssue({code:`custom`,message:`message_variant_link_labels_mismatch`,...i?{path:[...i,a,`linkLabels`]}:{}})}}const ge=e.array(C).min(1).superRefine((e,t)=>{new Set(e).size!==e.length&&t.addIssue({code:`custom`,message:`route_key_duplicate`})}),{routeKey:_e,...ve}=F.shape,L=F.shape.message,ye=e.object({...ve,contractVersion:e.literal(7),routeKeys:ge,message:L}).strict().superRefine(I),R=e.object({...ye.shape,contractVersion:e.literal(8),message:L.extend({channelVariants:e.partialRecord(e.enum(g),L.shape.variants).optional()})}).strict().superRefine((e,t)=>{I(e,t);let n=e.message.variants.map(e=>`${e.key}:${e.locale}`).sort(),r=e.message.bodies.map(e=>e.key),i=e.message.links.map(e=>e.key);for(let a of g){let o=e.message.channelVariants?.[a];if(!o)continue;let s=[`message`,`channelVariants`,a];JSON.stringify(o.map(e=>`${e.key}:${e.locale}`).sort())!==JSON.stringify(n)&&t.addIssue({code:`custom`,message:`message_channel_variant_languages_mismatch`,path:s}),he(o,r,i,t,s)}}),be=e.object({locale:y(35),contentLocale:y(35),title:y(160).refine(e=>e.trim().length>0,`browser_message_title_blank`),body:e.string().max(8192),url:w.shape.url}).strict(),z=e.object({...R.shape,contractVersion:e.literal(9),audience:e.object({topics:e.array(y(128)).min(1).max(2).superRefine((e,t)=>{new Set(e).size!==e.length&&t.addIssue({code:`custom`,message:`audience_topic_duplicate`})})}).strict().optional(),browserMessages:e.array(be).min(1).max(5).superRefine((e,t)=>{new Set(e.map(e=>e.locale)).size!==e.length&&t.addIssue({code:`custom`,message:`browser_message_locale_duplicate`})}).optional()}).strict().superRefine((e,t)=>{let{audience:n,browserMessages:r,...i}=e,o=R.safeParse({...i,contractVersion:8});if(o.success)E(e,t,a,65536);else for(let e of o.error.issues)t.addIssue({...e});r!==void 0&&n===void 0&&t.addIssue({code:`custom`,message:`browser_message_audience_missing`,path:[`audience`]})}),B=e.object({offset:e.number().int().nonnegative(),length:e.number().int().positive()}).strict();function V(e,t,n,r){let i=t?.map((e,t)=>({...e,index:t})).sort((e,t)=>e.offset-t.offset)??[],a=t=>!(t>0&&t<e.length&&/[\uD800-\uDBFF]/u.test(e.charAt(t-1))&&/[\uDC00-\uDFFF]/u.test(e.charAt(t))),o=0;for(let t of i){let i=t.offset+t.length;i>e.length?n.addIssue({code:`custom`,message:`quote_range_out_of_bounds`,path:[...r,t.index]}):(!a(t.offset)||!a(i))&&n.addIssue({code:`custom`,message:`quote_range_splits_code_point`,path:[...r,t.index]}),t.offset<o&&n.addIssue({code:`custom`,message:`quote_range_overlap`,path:[...r,t.index]}),o=Math.max(o,i)}}const xe=D.options[0].extend({quotes:e.array(B).optional()}).superRefine((e,t)=>V(e.value,e.quotes,t,[`quotes`])),Se=e.discriminatedUnion(`type`,[xe,D.options[1]]),Ce=e.array(Se).max(30),we={titleQuotes:e.array(B).optional(),sections:Ce},Te=e.array(e.discriminatedUnion(`key`,[L.shape.variants.element.options[0].extend(we),L.shape.variants.element.options[1].extend(we)])).min(1).max(2).superRefine((e,t)=>{for(let[n,r]of e.entries())V(r.title,r.titleQuotes,t,[n,`titleQuotes`])}),Ee=R.shape.message.extend({bodies:e.array(L.shape.bodies.element.extend({quotes:e.array(B).optional()}).superRefine((e,t)=>V(e.value,e.quotes,t,[`quotes`]))).max(2),variants:Te,channelVariants:e.partialRecord(e.enum(g),Te).optional()});function De(e){let{titleQuotes:t,...n}=e;return{...n,sections:n.sections.map(e=>{if(e.type!==`text`)return e;let{quotes:t,...n}=e;return n})}}const Oe=e.object({...z.shape,contractVersion:e.literal(10),message:Ee}).strict().superRefine((e,t)=>{let n=z.safeParse({...e,contractVersion:9,message:{...e.message,bodies:e.message.bodies.map(({quotes:e,...t})=>t),variants:e.message.variants.map(De),...e.message.channelVariants===void 0?{}:{channelVariants:Object.fromEntries(Object.entries(e.message.channelVariants).map(([e,t])=>[e,t.map(De)]))}}});if(!n.success){for(let e of n.error.issues)t.addIssue({...e});return}let{audience:r,browserMessages:o,...s}=e;E(s,t,a,i),E(e,t,a,65536)}),ke=e.object({type:e.literal(`creator.draft.v1`),templateId:e.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/),signalType:e.string().regex(/^[a-z][a-z0-9_]*$/),sourceTextZh:y(o),sourceUrl:w.shape.url.optional()}).strict();e.object({contractVersion:e.literal(1),eventType:e.literal(`local-task.delivery.requested`),eventId:e.string().uuid(),producer:x,routeKey:C,occurredAt:b,requestedAt:b,deliverBy:b,subject:S.extend({type:e.literal(`creator-event`)}),task:ke}).strict().superRefine((e,t)=>E(e,t,6048e5,o));const Ae=e.union([ie,ce,de,pe,P,F,ye,R,z,Oe]),je=e.object({destinationKey:e.string().min(1),state:e.enum(d),attempts:e.number().int().min(0).max(4),failureCode:e.enum(p).nullable(),lastAttemptAt:b.nullable(),nextAttemptAt:b.nullable(),leaseExpiresAt:b.nullable()}).strict(),H=e.object({contractVersion:e.literal(1),eventId:e.string().uuid(),producer:x,routeKey:C,subject:S,state:e.enum(s),reason:e.enum(c),terminal:e.boolean(),acceptedAt:b,updatedAt:b,deliverBy:b,terminalAt:b.nullable(),destinations:e.array(je).min(1).max(20)}).strict(),U=e.object({name:e.string().min(1).max(100),message:e.string().max(500).optional(),code:e.union([e.string().max(100),e.number()]).optional(),status:e.union([e.string().max(100),e.number()]).optional()}).strict(),Me=U.extend({cause:U.extend({cause:U.extend({cause:U.optional()}).optional()}).optional()}),Ne=je.extend({state:e.enum(f),attempts:e.number().int().min(0),revision:e.number().int().min(0),cause:Me.nullable(),unconfirmedAt:b.nullable()}),W=H.extend({contractVersion:e.literal(2),state:e.enum(l),reason:e.enum(u),revision:e.number().int().min(0),destinations:e.array(Ne).min(1).max(20)}),Pe=W.omit({routeKey:!0}).extend({contractVersion:e.literal(3),routeKeys:ge}),G=e.union([H,W,Pe]),Fe=e.object({path:e.array(e.union([e.string(),e.number().int().min(0)])).max(16),code:e.enum(h)}).strict(),Ie=e.object({code:e.literal(`invalid_request`),issues:e.array(Fe).max(20).optional()}).strict(),Le=e.object({code:e.enum(m.filter(e=>e!==`invalid_request`))}).strict(),Re=e.union([Ie,Le]);e.discriminatedUnion(`status`,[e.object({status:e.literal(`accepted`),receipt:H}).strict(),e.object({status:e.literal(`rejected`),error:Re}).strict()]),e.discriminatedUnion(`status`,[e.object({status:e.literal(`found`),receipt:H}).strict(),e.object({status:e.literal(`not_found`)}).strict(),e.object({status:e.literal(`unavailable`),error:e.object({code:e.literal(`receipt_unavailable`)}).strict()}).strict()]),e.discriminatedUnion(`status`,[e.object({status:e.literal(`accepted`),receipt:G}).strict(),e.object({status:e.literal(`rejected`),error:Re}).strict()]),e.discriminatedUnion(`status`,[e.object({status:e.literal(`found`),receipt:G}).strict(),e.object({status:e.literal(`not_found`)}).strict(),e.object({status:e.literal(`unavailable`),error:e.object({code:e.literal(`receipt_unavailable`)}).strict()}).strict()]);function ze(e,t){if((e.contractVersion===8||e.contractVersion===9||e.contractVersion===10)&&t!==void 0){let n=e.message.channelVariants?.[t];if(n!==void 0)return n}return e.message.variants}function K(e,t=[`source`],n){let r=Ae.parse(e),i=n===`tg`?t.toSorted((e,t)=>Number(e!==`source`)-Number(t!==`source`)):t;if(r.contractVersion===6||r.contractVersion===7||r.contractVersion===8||r.contractVersion===9||r.contractVersion===10){let e=ze(r,n),t=r.message.bodies;return{links:[],linksPerVariant:!0,variants:q(e.map(e=>({key:e.key,locale:e.locale,title:e.title,...e.titleQuotes===void 0?{}:{titleQuotes:e.titleQuotes},sections:e.sections,footer:e.footer,bodies:e.bodyRefs.map(e=>{let n=t.find(t=>t.key===e.key);return{label:e.label,value:n.value,...n.quotes===void 0?{}:{quotes:n.quotes}}}),links:r.message.links.map((t,n)=>({...t,label:e.linkLabels[n].label}))})),i)}}return r.contractVersion===1?{links:[...r.message.sourceLink?[{key:`source`,...r.message.sourceLink}]:[],{key:`primary`,...r.message.primaryLink}],linksPerVariant:!1,variants:[{key:`source`,locale:`und`,sections:[{key:`summary`,type:`text`,value:r.message.summary},...r.message.facts.length>0?[{key:`facts`,type:`fields`,items:r.message.facts}]:[]],title:r.message.title}]}:r.contractVersion===3||r.contractVersion===4?{links:r.message.links,linksPerVariant:!1,variants:q(r.message.variants,i)}:r.contractVersion===5?{links:[],linksPerVariant:!0,variants:q(r.message.variants.map(e=>({key:e.key,locale:e.locale,sections:e.sections,title:e.title,body:{label:e.bodyLabel,value:r.message.body.value},links:r.message.links.map((t,n)=>({...t,label:e.linkLabels[n].label}))})),i)}:{links:[...r.message.sourceLink?[{key:`source`,...r.message.sourceLink}]:[],{key:`primary`,...r.message.primaryLink}],linksPerVariant:!1,variants:q(r.message.variants.map(e=>({key:e.key,locale:e.locale,sections:[{key:`summary`,type:`text`,value:e.summary},...e.facts.length>0?[{key:`facts`,type:`fields`,items:e.facts}]:[]],title:e.title})),i)}}function q(e,t){let n=new Map(e.map(e=>[e.key,e])),r=[...new Set(t)].flatMap(e=>{let t=n.get(e);return t?[t]:[]});return r.length>0?r:[n.get(`source`)]}function Be(e,t=[`source`],n){return K(e,t,n).variants.map(e=>e.key)}var J=class extends Error{code=`mandatory_content_too_large`;constructor(){super(`generic_notification_mandatory_content_too_large`),this.name=`GenericNotificationRenderError`}};function Ve(e,t,n=[`source`],r={}){return Y(e,t,n,r)}function He(e,t,n=[`source`],r={}){return Y(e,t,n,r).text}function Y(e,t,n,r){if(!Number.isSafeInteger(t)||t<1)throw Error(`generic_notification_plain_text_limit_invalid`);let i=K(e,n,r.channel);if(e.contractVersion===6||e.contractVersion===7||e.contractVersion===8||e.contractVersion===9||e.contractVersion===10)return Ge(i.variants,t,r);let a=i.links.map(e=>$(e.label,e.url)),o=a.length>0?`\n\n${a.join(`
2
+
3
+ `)}`:``,s=i.linksPerVariant?``:o,c=r.variantSuffixes?.[i.variants.at(-1).key]??``,l=c.length>0?``:s,u=`
4
+
5
+ ---
6
+
7
+ `,d=i.variants.map((e,t)=>{let n=`${t===i.variants.length-1&&c.length>0?s:``}${r.variantSuffixes?.[e.key]??``}`,a=r.omitFirstTitle===!0&&t===0,o=e.sections.map(Ue).join(`
8
+
9
+ `),l=e.links??i.links,u=l.length>0?`\n\n${l.map(e=>$(e.label,e.url)).join(`
10
+
11
+ `)}`:``;return e.body===void 0?{fixed:a?``:e.title,optional:o.length===0?``:`${a?``:`
12
+
13
+ `}${o}`,minimum:0,suffix:`${i.linksPerVariant?u:``}${n}`}:{fixed:`${a?``:`${e.title}\n\n`}${o}\n\n${e.body.label===void 0?``:`${e.body.label}\n`}`,optional:e.body.value,minimum:1,suffix:`${i.linksPerVariant?u:``}${n}`}}),f=d.reduce((e,t)=>e+Q(t.fixed)+t.minimum,Q(u)*(d.length-1)+Q(l)+d.reduce((e,t)=>e+Q(t.suffix),0));if(f>t)throw new J;let p=d.map(e=>Q(e.optional)),m=d.map(e=>e.minimum),h=t-f;for(;h>0;){let e=p.map((e,t)=>({demand:e,index:t})).filter(({demand:e,index:t})=>e>m[t]);if(e.length===0)break;let t=Math.max(1,Math.floor(h/e.length));for(let{demand:n,index:r}of e){let e=Math.min(t,h,n-m[r]);m[r]+=e,h-=e}}return{text:`${d.map((e,t)=>`${e.fixed}${Ke(e.optional,m[t])}${e.suffix}`).join(u)}${l}`,quotes:[]}}function Ue(e){return e.type===`fields`?e.items.map(e=>`${e.label}: ${e.value}`).join(`
14
+ `):e.label?`${e.label}\n${e.value}`:e.value}function We(e,t){let n=e.map(()=>0);for(;t>0;){let r=e.map((e,t)=>({demand:e,index:t})).filter(({demand:e,index:t})=>e>n[t]);if(r.length===0)break;let i=Math.max(1,Math.floor(t/r.length));for(let{demand:e,index:a}of r){let r=Math.min(i,t,e-n[a]);n[a]+=r,t-=r}}return n}function Ge(e,t,n){let r=`
15
+
16
+ ---
17
+
18
+ `,i=e.map((e,t)=>{let r=e.bodies,i=Z([...n.omitFirstTitle&&t===0?[]:[X(e.title,e.titleQuotes)],...e.sections.map(e=>e.type===`fields`?X(Ue(e)):Z([...e.label?[X(e.label)]:[],X(e.value,e.quotes)],`
19
+ `))],`
20
+
21
+ `),a=[...(e.links??[]).map(e=>$(e.label,e.url)),...e.footer===void 0?[]:[e.footer]].map(e=>`\n\n${e}`).join(``)+(n.variantSuffixes?.[e.key]??``),o=r.map((e,t)=>e.label===void 0?i.text.length===0&&t===0?``:`
22
+
23
+ `:`\n\n${e.label}\n`),s=e.locale===`zh-CN`?`……(已截断)`:`… (truncated)`;return{bodies:r,prefix:i,suffix:a,labels:o,marker:s,minimums:r.map(e=>Math.min(Q(e.value),Q(s)))}}),a=Q(r)*(i.length-1)+i.reduce((e,t)=>e+Q(t.prefix.text+t.suffix+t.labels.join(``))+t.minimums.reduce((e,t)=>e+t,0),0);if(a>t)throw new J;let o=We(i.map(e=>e.bodies.reduce((t,n,r)=>t+Q(n.value)-e.minimums[r],0)),t-a);return Z(i.map((e,t)=>{let n=We(e.bodies.map((t,n)=>Q(t.value)-e.minimums[n]),o[t]),r=e.bodies.map((t,r)=>{let i=e.minimums[r]+n[r];if(Q(t.value)<=i)return Z([X(e.labels[r]),X(t.value,t.quotes)]);let a=Math.max(0,i-Q(e.marker)),o=[...t.value].slice(0,a).join(``),s=Math.max(o.lastIndexOf(`
24
+ `),o.lastIndexOf(`。`),o.lastIndexOf(`. `)),c=s>=o.length*.8?o.slice(0,s+1):o;return Z([X(e.labels[r]),X(c,t.quotes),X(e.marker)])});return Z([e.prefix,...r,X(e.suffix)])}),r)}function X(e,t=[]){return{text:e,quotes:t.flatMap(t=>{let n=Math.min(e.length,t.offset+t.length)-t.offset;return n>0?[{offset:t.offset,length:n}]:[]}).sort((e,t)=>e.offset-t.offset)}}function Z(e,t=``){let n=``,r=[];for(let[i,a]of e.entries())i>0&&(n+=t),r.push(...a.quotes.map(e=>({offset:n.length+e.offset,length:e.length}))),n+=a.text;return{text:n,quotes:r}}function Q(e){return[...e].length}function $(e,t){return`${e}: ${new URL(t).toString()}`}function Ke(e,t){if(t<=0)return``;let n=[...e];return n.length<=t?e:t===1?`…`:`${n.slice(0,t-1).join(``)}…`}export{J as GenericNotificationRenderError,Ve as renderGenericNotificationAnnotatedText,He as renderGenericNotificationPlainText,ze as resolveGenericNotificationChannelVariants,K as resolveGenericNotificationMessage,Be as resolveGenericNotificationMessageVariantKeys};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@closerepo/notification-contracts",
3
- "version": "1.12.0",
3
+ "version": "1.14.0",
4
4
  "type": "module",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -13,12 +13,18 @@
13
13
  },
14
14
  "files": [
15
15
  "dist/index.d.ts",
16
- "dist/index.js"
16
+ "dist/index.js",
17
+ "dist/render.d.ts",
18
+ "dist/render.js"
17
19
  ],
18
20
  "exports": {
19
21
  ".": {
20
22
  "types": "./dist/index.d.ts",
21
23
  "import": "./dist/index.js"
24
+ },
25
+ "./render": {
26
+ "types": "./dist/render.d.ts",
27
+ "import": "./dist/render.js"
22
28
  }
23
29
  },
24
30
  "dependencies": {