Skip to content

Commit

Permalink
Merge pull request #8 from 9vs/main
Browse files Browse the repository at this point in the history
allow specifying custom api url
  • Loading branch information
driaug authored Oct 15, 2024
2 parents 108b5a1 + b58bd60 commit 6dc3ff5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ Any interaction you want to make with the Plunk API needs to be done through the
```js
import Plunk from '@plunk/node';

// Using the default API URL
const plunk = new Plunk("Your secret key");

// Or specifying a custom API URL
const plunkCustom = new Plunk("Your secret key", { baseUrl: "https://selfhosted.example.com/api/v1/" });
```

## API
Expand Down
10 changes: 6 additions & 4 deletions src/lib/Plunk.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { PublishParams } from "../types/events";
import { SendParams } from "../types/emails";
import type { PublishParams } from "../types/events";
import type { SendParams } from "../types/emails";
import { NotFoundError } from "../errors/NotFound";
import { TokenError } from "../errors/TokenError";

export class Plunk {
private readonly key: string;
private readonly apiUrl: string;

private async fetch<T>({
json,
Expand All @@ -16,7 +17,7 @@ export class Plunk {
headers?: Record<string, string>;
}) {
const res = await fetch(
new URL(url, "https://api.useplunk.com/v1/").toString(),
new URL(url, this.apiUrl).toString(),
{
...options,
headers: {
Expand Down Expand Up @@ -46,8 +47,9 @@ export class Plunk {
return data as T;
}

constructor(key: string) {
constructor(key: string, options: { baseUrl?: string } = {}) {
this.key = key;
this.apiUrl = options.baseUrl || "https://api.useplunk.com/v1/";
}

/**
Expand Down

0 comments on commit 6dc3ff5

Please sign in to comment.