Edits history of script submission #15264 for ' Get audit records for time period (confluence)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Confluence = {
    	email: string
    	apiToken: string
    	domain: string
    }
    /**
     * Get audit records for time period
     * Returns records from the audit log, for a time period back from the current
    date.
     */
    export async function main(
    	auth: Confluence,
    	number: string | undefined,
    	units:
    		| 'NANOS'
    		| 'MICROS'
    		| 'MILLIS'
    		| 'SECONDS'
    		| 'MINUTES'
    		| 'HOURS'
    		| 'HALF_DAYS'
    		| 'DAYS'
    		| 'WEEKS'
    		| 'MONTHS'
    		| 'YEARS'
    		| 'DECADES'
    		| 'CENTURIES'
    		| undefined,
    	searchString: string | undefined,
    	start: string | undefined,
    	limit: string | undefined
    ) {
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/audit/since`)
    	for (const [k, v] of [
    		['number', number],
    		['units', units],
    		['searchString', searchString],
    		['start', start],
    		['limit', limit]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 235 days ago