Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to authorization header #87

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ npm install convertapi --save

## Usage

The package needs to be configured with your account's secret key which is available at https://www.convertapi.com/a
The package needs to be configured with your account's secret key or token which is available at https://www.convertapi.com/a

Require it with the key's value:

```javascript
var convertapi = require('convertapi')('your-api-secret');
var convertapi = require('convertapi')('your-api-secret-or-token');
```

Or using ES modules:

```javascript
import ConvertAPI from 'convertapi';

const convertapi = new ConvertAPI('your-api-secret');
const convertapi = new ConvertAPI('your-api-secret-or-token');
```

You can specify additional options, like proxy configuration and timeouts, when initializing the client:

```javascript
var convertapi = require('convertapi')('your-api-secret', {
var convertapi = require('convertapi')('your-api-secret-or-token', {
conversionTimeout: 60,
uploadTimeout: 60,
downloadTimeout: 60,
Expand All @@ -54,7 +54,7 @@ var convertapi = require('convertapi')('your-api-secret', {
If using ES module:

```javascript
const convertapi = new ConvertAPI('your-api-secret', { conversionTimeout: 60 });
const convertapi = new ConvertAPI('your-api-secret-or-token', { conversionTimeout: 60 });
```

### File conversion
Expand Down
2 changes: 1 addition & 1 deletion examples/alternative_converter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

// set your api secret
// set your api secret or token
var convertapi = require('../lib')(process.env.CONVERT_API_SECRET);

// Example of saving Word docx to PDF using OpenOffice converter
Expand Down
2 changes: 1 addition & 1 deletion examples/conversion_chaining.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

// set your api secret
// set your api secret or token
var convertapi = require('../lib')(process.env.CONVERT_API_SECRET);

// Short example of conversions chaining, the PDF pages extracted and saved as separated JPGs and then ZIP'ed
Expand Down
2 changes: 1 addition & 1 deletion examples/convert_base64_content.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

// set your api secret
// set your api secret or token
var convertapi = require('../lib')(process.env.CONVERT_API_SECRET);

// Example of using buffer and stream to convert base64 encoded content to pdf
Expand Down
2 changes: 1 addition & 1 deletion examples/convert_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var fs = require('fs');

// set your api secret
// set your api secret or token
var convertapi = require('../lib')(process.env.CONVERT_API_SECRET);

// Example of using readable stream to convert to pdf
Expand Down
2 changes: 1 addition & 1 deletion examples/convert_url_to_pdf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

// set your api secret
// set your api secret or token
var convertapi = require('../lib')(process.env.CONVERT_API_SECRET);

// Example of converting Web Page URL to PDF file
Expand Down
2 changes: 1 addition & 1 deletion examples/convert_word_to_pdf_and_png.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

// set your api secret
// set your api secret or token
var convertapi = require('../lib')(process.env.CONVERT_API_SECRET);

// Example of saving Word docx to PDF and to PNG
Expand Down
2 changes: 1 addition & 1 deletion examples/create_pdf_thumbnail.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

// set your api secret
// set your api secret or token
var convertapi = require('../lib')(process.env.CONVERT_API_SECRET);

// Example of extracting first page from PDF and then chaining conversion PDF page to JPG.
Expand Down
2 changes: 1 addition & 1 deletion examples/get_result_file_stream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

// set your api secret
// set your api secret or token
var convertapi = require('../lib')(process.env.CONVERT_API_SECRET);

// Example of converting Web Page URL to PDF and reading file data as string
Expand Down
2 changes: 1 addition & 1 deletion examples/retrieve_user_information.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

// set your api secret
// set your api secret or token
var convertapi = require('../lib')(process.env.CONVERT_API_SECRET);

// Retrieve user information
Expand Down
2 changes: 1 addition & 1 deletion examples/split_and_merge_pdf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

// set your api secret
// set your api secret or token
var convertapi = require('../lib')(process.env.CONVERT_API_SECRET);

// Example of extracting first and last pages from PDF and then merging them back to new PDF.
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface Client {

export class ConvertAPI {
client: Client;
constructor (secret: string, options?: Options);
constructor (credentials: string, options?: Options);
convert(toFormat: string, params: object, fromFormat?: string, conversionTimeout?: number): Promise<Result>;
getUser(): Promise<object>;
upload(source: string | NodeJS.ReadableStream, fileName?: string): Promise<UploadResult>;
Expand Down
3 changes: 2 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class Client {
this.defaultHeader = {
'User-Agent': api.userAgent,
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${api.credentials}`,
Accept: 'application/json',
};
this.httpsAgent = new https.Agent({ keepAlive: api.keepAlive });
Expand Down Expand Up @@ -93,7 +94,7 @@ export default class Client {
}

url(path) {
return `${this.api.baseUri}${path}?secret=${this.api.secret}`;
return `${this.api.baseUri}${path}`;
}

buildOptions(options) {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import Task from './task';
import Client from './client';
import { getReadableStream, normalizeBaseUri } from './utils';

function ConvertAPI(secret, options = {}) {
function ConvertAPI(credentials, options = {}) {
if (!(this instanceof ConvertAPI)) {
return new ConvertAPI(secret, options);
return new ConvertAPI(credentials, options);
}

this.secret = secret;
this.credentials = credentials;
this.baseUri = normalizeBaseUri(options.baseUri || 'https://v2.convertapi.com/');
this.conversionTimeout = options.conversionTimeout;
this.conversionTimeoutDelta = options.conversionTimeoutDelta || 10;
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ const api = new ConvertAPI(process.env.CONVERT_API_SECRET);

describe('ConvertAPI', () => {
it('should be accessible as function', () => {
expect(ConvertAPI('test').secret).to.equal('test');
expect(ConvertAPI('test').credentials).to.equal('test');
});

it('should assign secret', () => {
it('should assign credentials', () => {
const expectedVal = process.env.CONVERT_API_SECRET;
expect(api.secret).to.equal(expectedVal);
expect(api.credentials).to.equal(expectedVal);
});

it ('should upload file', () => {
Expand Down
Loading