@@ -20,6 +20,7 @@ import type {
2020 DocuSignSendEnvelopeParams ,
2121 DocuSignVoidEnvelopeParams ,
2222} from '@/tools/docusign/types'
23+ import { safeUrlPathSegment } from '@/tools/url-path'
2324
2425const logger = createLogger ( 'DocuSignOperations' )
2526const MAX_LEGACY_INLINE_DOCUMENT_BYTES = 7 * 1024 * 1024
@@ -37,6 +38,14 @@ function jsonBody(data: Record<string, unknown>): RequestInit {
3738 return { method : 'POST' , body : JSON . stringify ( data ) }
3839}
3940
41+ function pathSegment ( value : string , paramName : string ) : string {
42+ try {
43+ return safeUrlPathSegment ( value , paramName )
44+ } catch ( error ) {
45+ throw new DocuSignOperationError ( getErrorMessage ( error , `Invalid ${ paramName } ` ) , 400 )
46+ }
47+ }
48+
4049async function client (
4150 input : { accessToken : string ; instanceUrl ?: string } ,
4251 signal ?: AbortSignal
@@ -141,6 +150,7 @@ export async function executeDocuSignSendEnvelope(
141150 anchorUnits : 'pixels' ,
142151 anchorXOffset : '0' ,
143152 anchorYOffset : '0' ,
153+ anchorIgnoreIfNotPresent : 'true' ,
144154 } ,
145155 ] ,
146156 dateSignedTabs : [
@@ -149,6 +159,7 @@ export async function executeDocuSignSendEnvelope(
149159 anchorUnits : 'pixels' ,
150160 anchorXOffset : '0' ,
151161 anchorYOffset : '0' ,
162+ anchorIgnoreIfNotPresent : 'true' ,
152163 } ,
153164 ] ,
154165 } ,
@@ -191,9 +202,10 @@ export async function executeDocuSignGetEnvelope(
191202 input : DocuSignGetEnvelopeParams ,
192203 context : DocuSignOperationContext
193204) {
205+ const envelopeId = pathSegment ( input . envelopeId , 'envelopeId' )
194206 const provider = await client ( input , context . signal )
195207 return provider . json (
196- `/envelopes/${ input . envelopeId . trim ( ) } ?include=recipients,documents` ,
208+ `/envelopes/${ envelopeId } ?include=recipients,documents` ,
197209 { } ,
198210 'DocuSign envelope response' ,
199211 'Failed to get envelope' ,
@@ -216,6 +228,7 @@ export async function executeDocuSignListEnvelopes(
216228 if ( input . envelopeStatus ) query . append ( 'status' , input . envelopeStatus )
217229 if ( input . searchText ) query . append ( 'search_text' , input . searchText )
218230 if ( input . count ) query . append ( 'count' , input . count )
231+ if ( input . startPosition ) query . append ( 'start_position' , input . startPosition )
219232 const provider = await client ( input , context . signal )
220233 return provider . json (
221234 `/envelopes?${ query } ` ,
@@ -230,9 +243,10 @@ export async function executeDocuSignVoidEnvelope(
230243 input : DocuSignVoidEnvelopeParams ,
231244 context : DocuSignOperationContext
232245) {
246+ const envelopeId = pathSegment ( input . envelopeId , 'envelopeId' )
233247 const provider = await client ( input , context . signal )
234248 await provider . json (
235- `/envelopes/${ input . envelopeId . trim ( ) } ` ,
249+ `/envelopes/${ envelopeId } ` ,
236250 { method : 'PUT' , body : JSON . stringify ( { status : 'voided' , voidedReason : input . voidedReason } ) } ,
237251 'DocuSign void envelope response' ,
238252 'Failed to void envelope' ,
@@ -245,10 +259,11 @@ export async function executeDocuSignDownloadDocument(
245259 input : DocuSignDownloadDocumentParams ,
246260 context : DocuSignOperationContext
247261) {
248- const documentId = input . documentId || 'combined'
262+ const envelopeId = pathSegment ( input . envelopeId , 'envelopeId' )
263+ const documentId = pathSegment ( input . documentId || 'combined' , 'documentId' )
249264 const provider = await client ( input , context . signal )
250265 const { buffer, contentType, fileName } = await provider . document (
251- input . envelopeId . trim ( ) ,
266+ envelopeId ,
252267 documentId ,
253268 context . signal
254269 )
@@ -281,6 +296,7 @@ export async function executeDocuSignListTemplates(
281296 const query = new URLSearchParams ( )
282297 if ( input . searchText ) query . append ( 'search_text' , input . searchText )
283298 if ( input . count ) query . append ( 'count' , input . count )
299+ if ( input . startPosition ) query . append ( 'start_position' , input . startPosition )
284300 const suffix = query . size ? `?${ query } ` : ''
285301 const provider = await client ( input , context . signal )
286302 return provider . json (
@@ -296,9 +312,10 @@ export async function executeDocuSignListRecipients(
296312 input : DocuSignListRecipientsParams ,
297313 context : DocuSignOperationContext
298314) {
315+ const envelopeId = pathSegment ( input . envelopeId , 'envelopeId' )
299316 const provider = await client ( input , context . signal )
300317 return provider . json (
301- `/envelopes/${ input . envelopeId . trim ( ) } /recipients` ,
318+ `/envelopes/${ envelopeId } /recipients` ,
302319 { } ,
303320 'DocuSign recipients response' ,
304321 'Failed to list recipients' ,
0 commit comments