submitjson
TypeScript icon, indicating that this package has built-in type declarations

0.10.0 • Public • Published

submitjson

npm npm package minimized gzipped size (select exports)

JavaScript client for Submit JSON written in TypeScript. Works in modern browsers, as well as runtimes like Node.js v18+, Bun, Deno, and Edge Runtime.

Quick start

If you haven't already, sign up for a Submit JSON account.

Install the client with a package manager:

npm install submitjson # || pnpm add submitjson || yarn add submitjson

Import and create a new client instance

import SubmitJSON from 'submitjson'

const sj = new SubmitJSON({
  apiKey: 'sjk_xxxxxxxxxxxxxx',
  endpoint: 'XxXxXxXxX'
})

const data = await sj.submit({
  name: 'Yo Yoerson',
  message: 'Yo',
  powerLevel: 9001,
})

console.log('Submission', data)

Table of Contents

Configuration

  • Details

    Import and create a new Submit JSON client instance. We recommend including your endpoint here for easier submit calls down the line. Pass in default options per client to override the current endpoint settings.

  • Type

    interface SubmitJSONConfig {
      apiKey: string
      endpoint?: string
      options: SubmitOptions
    }
    
    interface SubmitOptions {
      emailNotification?: boolean
      emailTo?: string
      emailReplyTo?: string
      emailBranding?: boolean
      emailSubject?: string
      emailFromName?: string
      submissionFormat?: 'pretty' | 'raw'
      submissionSound?: 'none' | 'beep' | 'blip' | 'block' | 'coin' | 'ding' | 'dink' | 'honk' | 'jump' | 'ping' | 'pong' | 'snare'
      recaptchaToken?: string
      turnstileToken?: string
      hcaptchaToken?: string
    }
    
    class SubmitJSON {
      constructor(config: SubmitJSONConfig)
      submit(data, options, endpoint): Promise<Submission>
    }
  • Example

    // ~/submitjson.ts
    import SubmitJSON from 'submitjson'
    
    export const sj = new SubmitJSON({
      apiKey: 'sjk_xxxxxxxxxxxxxx',
      endpoint: 'XxXxXxXxX',
      options: { // set defaults for this client & override endpoint settings
        emailNotification: true,
        submissionFormat: 'raw',
        submissionSound: 'none',
      },
    })

API

submit()

  • Details

    POST your data to an endpoint and get notified in real time.

    submit() takes three arguments:

    1. The data (must be a valid JSON object, JSON string, or FormData)
    2. Optional configuration to override the endpoint's default settings. If this argument is a string it is treated as the endpoint for submitting data
    3. An optional endpoint.
  • Type

    function submit(
      data: Record<string, unknown> | string | FormData,
      options?: SubmitOptions,
      endpoint?: string
    ): Promise<Submission>
  • Example with all configuration options

    import SubmitJSON from 'submitjson'
    
    const sj = new SubmitJSON({
      apiKey: 'sjk_xxxxxxxxxxxxxx',
      endpoint: 'XxXxXxXxX',
    })
    
    const data = await sj.submit({
      name: 'Yo Yoerson',
      message: 'Yo',
      powerLevel: 9001,
    }, {
      emailNotification: true,
      emailTo: 'yo@yoerson.com',
      emailReplyTo: 'diff@differson.com',
      emailBranding: false,
      emailSubject: 'My custom subject line',
      emailFromName: 'My custom from name',
      submissionFormat: 'pretty',
      submissionSound: 'ping',
      recaptchaToken: 'xxxxxxxxxxx'
    }, 'YyYyYyYyY') // overrides the endpoint set in the configuration
    
    console.log('Submission', data)
  • Example with multiple clients

    Initialize multiple clients for a seperation of concerns.

    // submitjson.ts
    import SubmitJSON from 'submitjson'
    
    export const contactForm = new SubmitJSON({
      apiKey: 'sjk_xxxxxxxxxxxxxx',
      endpoint: 'XxXxXxXxX',
    })
    
    export const userSignupNotification = new SubmitJSON({
      apiKey: 'sjk_xxxxxxxxxxxxxx',
      endpoint: 'ZzZzZzZzZ',
    })
    // somewhere else in your code
    const data = { name: 'Yo Yoerson', message: 'Yo' }
    await contactForm.submit(data)
    await userSignupNotification.submit(data)

Submit JSON Links

License

MIT License © 2023 Submit JSON

Readme

Keywords

none

Package Sidebar

Install

npm i submitjson

Weekly Downloads

10

Version

0.10.0

License

MIT

Unpacked Size

33 kB

Total Files

15

Last publish

Collaborators

  • dylanmcgowan