Convert Zapier plugin schemas to Typescript definitions

Abe Wilson
Abe Wilson
Content writer @MailSlurp
  • Table of contents

How to convert Zapier plugin schemas to Typescript definitions using the official zapier schemas package

Recently I had to work on the MailSlurp zapier email plugin and realised we didn't have strong typescript definitions for our schemas. So I created this basic script to convert the official zapier schemas into Typescript.

The code

You'll need to set the WD environment variable to your working directory when running the script:

const wd = process.env.WD;
const fs = require('fs')
const { compile} =require('json-schema-to-typescript')
const zapierSchema = require('zapier-platform-schema/exported-schema.json')
const {join} = require("path");

const exportedSchema = JSON.parse(JSON.stringify(zapierSchema)
    .replace(/"$ref":\s+"\//g, '"$ref":"#/definitions/'))


const schema = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://json-schema.org/draft-07/schema#",
    name: 'AppSchema',
    definitions: Object.entries(exportedSchema.schemas).reduce((acc, obj) => {
        acc[obj[1].id.replace('/', '')] = obj[1]
        return acc;
    }, {}),
    ...({ ...exportedSchema.schemas.AppSchema, id: undefined })
}

compile(schema, {
})
    .then(ts => fs.writeFileSync(join(wd, 'types.d.ts'), ts))
    .then(() => console.log('Types written'))
    .catch(err => {
        console.error(err);
        process.exit(1);
    })
Email and SMS Platform
Create a free account in 3 clicks