with JLCPCB and Node?
Write custom Node.js code and use any of the 400k+ npm packages available. Refer to the Pipedream Node docs to learn more.
import { axios } from "@pipedream/platform"
import crypto from "crypto"
export default defineComponent({
props: {
jlcpcb: {
type: "app",
app: "jlcpcb",
}
},
async run({ steps, $ }) {
const appCtx = this;
function generateNonce() {
return crypto.randomBytes(16).toString('hex');
}
function buildStringToSign(method, path, timestamp, nonce, body = '') {
return `${method.toUpperCase()}\n${path}\n${timestamp}\n${nonce}\n${body}\n`;
}
function signString(stringToSign, secretKey) {
const hmac = crypto.createHmac('sha256', secretKey);
hmac.update(stringToSign, 'utf8');
return hmac.digest('base64');
}
function getAuthHeader(method, path, body, appId, accessKey, secretKey) {
const nonce = generateNonce();
const timestamp = Math.floor(Date.now() / 1000);
const stringToSign = buildStringToSign(method, path, timestamp, nonce, body);
const signature = signString(stringToSign, secretKey);
return `JOP appid="${appId}", accesskey="${accessKey}", nonce="${nonce}", timestamp="${timestamp}", signature="${signature}"`;
}
async function queryOrder() {
const appId = appCtx.jlcpcb.$auth.app_id;
const accessKey = appCtx.jlcpcb.$auth.access_key;
const secretKey = appCtx.jlcpcb.$auth.secret_key;
const method = "POST";
const path = "/overseas/openapi/pcb/order/detail";
const bodyObj = { batchNumber: "BATCH12345" };
const body = JSON.stringify(bodyObj);
const authHeader = getAuthHeader(method, path, body, appId, accessKey, secretKey);
const response = await axios($, {
method, url: `https://open.jlcpcb.com${path}`, data: bodyObj,
headers: {
"Content-Type": "application/json",
"Authorization": authHeader
}
});
return response;
}
return await queryOrder();
},
})
Develop, run and deploy your Node.js code in Pipedream workflows, using it between no-code steps, with connected accounts, or integrate Data Stores and File Stores
This includes installing NPM packages, within your code without having to manage a package.json file or running npm install.
Below is an example of installing the axios package in a Pipedream Node.js code step. Pipedream imports the axios package, performs the API request, and shares the response with subsequent workflow steps:
// To use previous step data, pass the `steps` object to the run() function
export default defineComponent({
async run({ steps, $ }) {
// Return data to use it in future steps
return steps.trigger.event
},
})