3.0.0 • Published 6 months ago

dpaste-ts v3.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

dpaste-ts

codecov

NPM

Nodejs wrapper for dpaste.com written using Typescript. Creates & Gets pastes anonymously.

Now with Authentication support!

Usage

The functions are promise based. Use them inside async functions or you may use .then().catch().

All functions have an internal delay of 1 second. This is to prevent abuse of dpaste.com API

Disable delay

Set the environment variable as follows -

DPASTE_DISABLE_DELAY = true

Note: if the rate of requests exceeds 1 request per second, then you will be blocked from API to create new pastes. Same applies for reading pastes.

Authentication

Read more here under Authentication section.

Generate an API token key from your dashboard.

Set it into your environment with the variable name as DPASTE_API_TOKEN

Import

const { createPaste, getRawPaste } = require('dpaste-ts'); // in CJS style

import { createPaste, getRawPaste } from 'dpaste-ts'; // in ESM style

Create Paste

Returns URL of dpaste.

There are 5 arguments:

ArgumentsTypeRange/LimitDefault valuesRequired?
contentstring250,000 bytesNot Applicabletrue
titlestringFirst 100 charactersdate & time of paste creation in UTCfalse
syntaxstringCheck here'text'false
expiry_daysnumber1 to 3657false
APITokenstringNot ApplicableValue set in environment variable DPASTE_API_TOKEN or undefinedfalse

When inside async function:

const options = {
  content: 'sample input',
  title: 'sample title',
  syntax: 'text',
  expiry_days: 10,
};

// Will return dpaste link when successful
let url = await createPaste(options);
console.log(url);

Using .then()

// Will return dpaste link to console
createPaste(options).then(console.log);

Get Raw Paste

Returns Raw data of dpaste.

There are 2 arguments:

ArgumentsTypeRequired?
url/idstringtrue
APITokenstringfalse

You can either provide complete URL of the dpaste or the ID of the paste.

When inside async function:

// Will return "sample input" as raw data
let rawData = await getRawPaste(url);
console.log(rawData);

Using .then()

// Will return raw data to console
getRawPaste(url).then(console.log);

Complete example

const { createPaste, getRawPaste } = require('dpaste-ts');

const options = {
  content: 'sample input',
  title: 'sample title',
  syntax: 'text',
  expiry_days: 10,
  APIToken: 'qwerty12345ag',
};
// This will set the API token into environment variable
process.env.DPASTE_API_TOKEN = options.APIToken;

// This will **not** disable internal delay of 1 second. Set it to true if you wish to disable it.
process.env.DPASTE_DISABLE_DELAY = 'false';

// Use the below code snippet inside async functions to get the data.

// Will return dpaste link when successful
let url = await createPaste(options);
console.log(url);

// Will return "sample input" as raw data
let rawData = await getRawPaste(url);
console.log(rawData);

// Use the below snippets if you wish to use .then()
// Put .catch() to catch any potential errors

// Will return dpaste link to console
createPaste(options).then(console.log);

// Will return raw data to console
getRawPaste(url).then(console.log);

Licence

MIT

3.0.0

6 months ago

2.0.0

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago