Skip to content

Commit

Permalink
fix typescript exports (#29, #28)
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerlepine authored Jan 3, 2025
2 parents d079c13 + dd691c5 commit 108af0a
Show file tree
Hide file tree
Showing 80 changed files with 1,299 additions and 1,600 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.0] - 2025-01-03

- Organized shared types and fixed bundle export
- Improved configuration object

## [1.1.0] - 2024-11-08

- Replaced `fetch` with `axios` for improved security
Expand Down
57 changes: 52 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,39 @@ The package needs to be configured with your account's Personal Access Token (cr
import Printify from 'printify-sdk-js';

const printify = new Printify({
shopId: '123456', // (optional) find using printify.shops.list()
accessToken: 'asdf0123asdf0123asdf0123', // generate a token: https://printify.com/app/account/api
enableLogging: true, // (optional) enabled by default
accessToken: process.env.PRINTIFY_API_TOKEN, // generate a token: https://printify.com/app/account/api
shopId: '123456', // (optional) find using `printify.shops.list()`
enableLogging: true,
});

const orders = await printify.orders.list({ limit: 5, status: 'fulfilled' });
console.log(orders); // { current_page: 1, data: [{ id: "5a9", address_to: {}, line_items: [], total_price: 2200, status: "fulfilled" } ]
```

### Usage with TypeScript

```typescript
import Printify from 'printify-sdk-js';
import type { ListWebhooksResponse, Webhook } from 'printify-sdk-js';

const printify = new Printify({
accessToken: process.env.PRINTIFY_API_TOKEN,
shopId: '123456',
});

const result: ListWebhooksResponse = await printify.webhooks.list();
const webhook: Webhook = result[0];
console.log(webhook); // { "topic": "order:created", "url": "https://example.com/webhooks/order/created", "shop_id": "1", "id": "5cb87a8cd490a2ccb256cec4" }
```

### Usage with CommonJS

```js
const Printify = require('printify-sdk-js');

const printify = new Printify({
accessToken: process.env.PRINTIFY_API_TOKEN,
shopId: '123456',
accessToken: 'asdf0123asdf0123asdf0123',
enableLogging: true,
});

printify.orders
Expand All @@ -65,6 +80,30 @@ printify.orders
.catch(error => console.error(error));
```

## Configuration

```js
import Printify from 'printify-sdk-js';

const printify = new Printify({
accessToken: process.env.PRINTIFY_API_TOKEN,
shopId: '123456',
enableLogging: true,
apiVersion: 'v1',
host: 'api.printify.com',
timeout: 5000, // in ms
});
```

| Option | Default | Description |
| --------------- | -------------------- | ----------------------------------------------------------------------------------------------------------------------- | --- |
| `accessToken` | `null` | The API access token for authenticating requests. Generate one at [Printify API](https://printify.com/app/account/api). |
| `shopId` | `null` | (optional) The ID of the shop to use for API requests. Can be found using `printify.shops.list()`. |
| `enableLogging` | `true` | (optional) Enables logging of API requests and responses. Enabled by default. |
| `apiVersion` | `'v1'` | (optional) The API version |
| `host` | `'api.printify.com'` | The host for API requests. | |
| `timeout` | `5000` | (optional) Reqeust timeout in ms |

## Development

```sh
Expand All @@ -74,6 +113,14 @@ yarn test

> If you do not have yarn installed, you can get it with `npm install --global yarn`.
```sh
# (optional) test the bundle locally
yarn build
mv dist examples/development
cd examples/development
yarn start
```

## Contributing

We welcome contributions from the community! If you're interested in contributing to this project, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) file to get started.
Expand Down
2 changes: 1 addition & 1 deletion examples/commonjs/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Printify = require('printify-sdk-js');

const printify = new Printify({
shopId: '123456',
accessToken: 'asdf0123asdf0123asdf0123',
accessToken: process.env.PRINTIFY_API_TOKEN, // UPDATE ME
enableLogging: true,
});

Expand Down
1 change: 1 addition & 0 deletions examples/development/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app.js
14 changes: 14 additions & 0 deletions examples/development/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @ts-expect-error - doesn't automatically detect "dist/index.d.mts"
import Printify from './dist/index.cjs.js';
import type { Webhook } from './dist/index.d.mts';

const printify = new Printify({
shopId: '123456',
accessToken: process.env.PRINTIFY_API_TOKEN,
enableLogging: true,
});

(async () => {
const result: Webhook[] = await printify.webhooks.list();
console.log(result);
})();
12 changes: 12 additions & 0 deletions examples/development/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "printify-development-testing",
"scripts": {
"start": "tsc && node app.js"
},
"dependencies": {
"printify-sdk-js": "*"
},
"devDependencies": {
"typescript": "^5.5.4"
}
}
10 changes: 10 additions & 0 deletions examples/development/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
2 changes: 1 addition & 1 deletion examples/node/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Printify from 'printify-sdk-js';

const printify = new Printify({
shopId: '123456',
accessToken: 'asdfasdf',
accessToken: process.env.PRINTIFY_API_TOKEN, // UPDATE ME
enableLogging: true,
});

Expand Down
41 changes: 5 additions & 36 deletions examples/typescript/app.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
import Printify from 'printify-sdk-js';
import type { ListWebhooksResponse, Webhook } from 'printify-sdk-js';

const printify = new Printify({
shopId: '123456',
accessToken: 'asdfasdf',
accessToken: process.env.PRINTIFY_API_TOKEN,
enableLogging: true,
});

const orderData = {
external_id: 'unique_external_id',
label: 'Order Label',
line_items: [
{
print_provider_id: 12345,
blueprint_id: 67890,
variant_id: 112233,
print_areas: {
front: 'https://example.com/path/to/sticker.png',
},
quantity: 1,
},
// ...
],
shipping_method: 1,
is_printify_express: true,
is_economy_shipping: false,
send_shipping_notification: true,
address_to: {
first_name: 'string',
last_name: 'string',
email: 'string',
phone: 'string',
region: 'NY',
address1: '123 Main Street',
address2: '',
city: 'New York',
zip: '10001',
country: 'US',
},
};

(async () => {
const result = await printify.orders.submit(orderData);
console.log(result); // { "id": "5a96f649b2439217d070f507" }
const result: ListWebhooksResponse = await printify.webhooks.list();
const webhook: Webhook = result[0];
console.log(webhook); // { "topic": "order:created", "url": "https://example.com/webhooks/order/created", "shop_id": "1", "id": "5cb87a8cd490a2ccb256cec4" }
})();
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ module.exports = {
transform: {
'^.+.tsx?$': ['ts-jest', {}],
},
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/*.d.ts'],
coverageThreshold: {
global: {
functions: 100,
},
},
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "printify-sdk-js",
"version": "1.1.0",
"description": "Printify SDK for JavaScript. Client for Node.js",
"version": "1.2.0",
"description": "Node.js SDK for the Printify API.",
"author": "Spencer Lepine <spencer.sayhello@gmail.com>",
"license": "MIT",
"homepage": "https://github.com/spencerlepine/printify-sdk-js#readme",
Expand Down Expand Up @@ -67,15 +67,16 @@
"axios": "^1.7.7"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"@jest/types": "^29.6.3",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/jest": "^29.5.14",
"@types/node": "^20.14.10",
"@typescript-eslint/eslint-plugin": "^5.8.0",
"@typescript-eslint/parser": "^5.8.0",
"eslint": "^8.0.0",
"eslint-plugin-prettier": "^5.2.1",
"husky": "4.3.8",
"jest": "^29.7.0",
"lint-staged": "10.5.4",
Expand All @@ -93,4 +94,4 @@
"engines": {
"node": ">= 18"
}
}
}
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import typescript from 'rollup-plugin-typescript2';
import dts from 'rollup-plugin-dts';

export default [
// Generate bundles for ESM/CommonJS
{
input: 'src/index.ts',
output: [
Expand Down Expand Up @@ -33,6 +34,7 @@ export default [
terser(),
],
},
// Generate a .d.ts file to group all types
{
input: 'dist/index.d.ts',
output: [{ file: 'dist/index.d.mts', format: 'es' }],
Expand Down
40 changes: 0 additions & 40 deletions src/catalog/index.ts

This file was deleted.

Loading

0 comments on commit 108af0a

Please sign in to comment.