1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- export const SalesforceApexCodegen = {
- id: "salesforce-apex",
- name: "Salesforce Apex",
- language: "apex",
- generator: ({
- url,
- pathName,
- queryString,
- auth,
- httpUser,
- httpPassword,
- bearerToken,
- method,
- rawInput,
- rawParams,
- rawRequestBody,
- contentType,
- headers,
- }) => {
- const requestString = []
- // initial request setup
- let requestBody = rawInput ? rawParams : rawRequestBody
- requestBody = JSON.stringify(requestBody)
- .replace(/^"|"$/g, "")
- .replace(/\\"/g, '"')
- .replace(/'/g, "\\'") // Apex uses single quotes for strings
- // create request
- requestString.push(`HttpRequest request = new HttpRequest();\n`)
- requestString.push(`request.setMethod('${method}');\n`)
- requestString.push(
- `request.setEndpoint('${url}${pathName}?${queryString}');\n\n`
- )
- // authentification
- if (auth === "Basic Auth") {
- const basic = `${httpUser}:${httpPassword}`
- requestString.push(
- `request.setHeader('Authorization', 'Basic ${window.btoa(
- unescape(encodeURIComponent(basic))
- )}');\n`
- )
- } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
- requestString.push(
- `request.setHeader('Authorization', 'Bearer ${bearerToken}');\n`
- )
- }
- // content type
- if (contentType) {
- requestString.push(
- `request.setHeader('Content-Type', '${contentType}');\n`
- )
- }
- // custom headers
- if (headers) {
- headers.forEach(({ key, value }) => {
- if (key) {
- requestString.push(`request.setHeader('${key}', '${value}');\n`)
- }
- })
- }
- requestString.push(`\n`)
- // set body
- if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
- requestString.push(`request.setBody('${requestBody}');\n\n`)
- }
- // process
- requestString.push(`try {\n`)
- requestString.push(` Http client = new Http();\n`)
- requestString.push(` HttpResponse response = client.send(request);\n`)
- requestString.push(` System.debug(response.getBody());\n`)
- requestString.push(`} catch (CalloutException ex) {\n`)
- requestString.push(
- ` System.debug('An error occurred ' + ex.getMessage());\n`
- )
- requestString.push(`}`)
- return requestString.join("")
- },
- }
|