Created
January 16, 2025 08:34
-
-
Save marcelklehr/ecfe72aa8b5f9717f8cb9fa6fbfbfd1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as wmill from "npm:windmill-client@1" | |
import axios from "npm:axios" | |
export async function main( | |
nextcloudResource: string, | |
userId: string|null = null, | |
templateFileId: number, | |
destination: string, | |
fields: object, | |
convertToPdf: boolean = false, | |
useAppApiAuth: boolean = false, | |
) { | |
const ncResource = await wmill.getResource( | |
nextcloudResource, | |
); | |
const data = { | |
fields: Object.keys(fields).reduce((carry: object, key: string) => { | |
carry['ContentControls.ByIndex.' + key] = { content: fields[key] } | |
return carry | |
}, {}), | |
destination, | |
} | |
if (convertToPdf) { | |
data.convert = 'pdf' | |
} | |
console.debug('data', data) | |
const url = ncResource.baseUrl + '/ocs/v2.php/apps/richdocuments/api/v1/template/fields/fill/' + templateFileId | |
const config = { | |
...(!useAppApiAuth && ({ | |
auth: { | |
username: ncResource.username, | |
password: ncResource.password, | |
}, | |
})), | |
headers: { | |
'content-type': 'application/json', | |
'ocs-apirequest': true, | |
...(useAppApiAuth && ({ | |
"AA-VERSION": "2.3.0", | |
"EX-APP-ID": "windmill_app", | |
"EX-APP-VERSION": "1.0.0", | |
"AUTHORIZATION-APP-API": btoa( | |
`${userId || ncResource.username}:${ncResource.password}`, | |
), | |
})), | |
}, | |
} | |
console.debug('config', config) | |
try { | |
const resp = await axios.post(url, data, config) | |
console.debug('RESPONSE', resp.data) | |
return { | |
data: resp.data, | |
} | |
} catch(e) { | |
console.debug('error', e) | |
} | |
return {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment