Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add tests + JS, add readme, rename to notifer, bump to 1.0.1
  • Loading branch information
Sticks committed Feb 13, 2023
1 parent e9d2aa7 commit 0b76b91
Show file tree
Hide file tree
Showing 10 changed files with 4,062 additions and 715 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,3 +1,4 @@
node_modules/
lib/
src/test.ts
src/playground.ts
coverage/
21 changes: 20 additions & 1 deletion README.md
@@ -1 +1,20 @@
# notifer.js
# notifer
A simple libary to interface with an NTFY/our own notification server.

# Features
[x] Publish notifications
[] Subcribe to a channel and listen to it's notifications

# Usage
Useage is really simple, just import the libary and use the publish function to send a notification to a channel.

```js
const { notifer } = require('@sticksdev/notifer')

const client = new notifer() // A url can also be passed here, if not, it defaults to our instance (notify.hep.gg)
client.REST.publishNotification('channelname', "test message", "Test Notification").then((res) => {
console.log(res) // Returns an APIBaseResponse or IAPIErrorResponse
})
```

All options and types are documented in the code, so you can use your IDE to get the types and option. If you have any questions/suggestions/bugs, feel free to open an issue.
37 changes: 37 additions & 0 deletions __tests__/REST.ts
@@ -0,0 +1,37 @@
import { notifer } from "../src/main";
import { APIBaseResponse } from "../src/objects/APIBaseResponse";
import { IAPIErrorResponse } from "../src/objects/APIErrorResponse";

const client = new notifer()
const randomChannelName = Math.random().toString(36).substring(7);

describe("REST", () => {
it("should return a promise on publishNotification", () => {
const promise = client.REST.publishNotification(`nullchannel-${randomChannelName}`, "JEST Test", "This is a test from JEST")
expect(promise).toBeInstanceOf(Promise);
});

it("should return an APIBaseResponse on publishNotification success", async () => {
const response = await client.REST.publishNotification(`nullchannel-${randomChannelName}`, "JEST Test", "This is a test from JEST")
expect(response).toBeInstanceOf(APIBaseResponse)
});

it("should return an IAPIErrorResponse on publishNotification failure", async () => {
// FIXME: Ntfy.sh returns an error on the JSON post endpoint ("Unauthorized") persumably because of access control rules
// But this may change in the future, so we should probably mock this
const tempclient = await new notifer("https://ntfy.sh")
const response = await tempclient.REST.publishNotification(`nullchannel-${randomChannelName}`, "JEST Test", "This is a test from JEST")

expect(response).toHaveProperty("error")
expect(response).toBeInstanceOf(IAPIErrorResponse)
});

it("should be able to set auth token", () => {
const tempclient = new notifer()
tempclient.REST.setBasicCreditentials("test", "test")

expect(tempclient.REST.basicAuthUsername).toBe("test")
expect(tempclient.REST.basicAuthPassword).toBe("test")
expect(tempclient.REST.useBasicAuth).toBe(true);
})
});
15 changes: 15 additions & 0 deletions __tests__/main.ts
@@ -0,0 +1,15 @@
import { RESTNotifyClient } from "../src/classes/REST";
import { notifer } from "../src/main";

const client = new notifer();

describe("Main Class", () => {
it("should be able to create a new instance of the main class", () => {
expect(client).toBeDefined();
});

it("should have a REST property", () => {
expect(client.REST).toBeDefined();
expect(client.REST).toBeInstanceOf(RESTNotifyClient);
});
});
11 changes: 11 additions & 0 deletions jest.config.ts
@@ -0,0 +1,11 @@
import type { Config } from "@jest/types"

const config: Config.InitialOptions = {
preset: "ts-jest",
testEnvironment: "node",
verbose: true,
collectCoverage: true,
modulePathIgnorePatterns: ["<rootDir>/lib"],
}

export default config

0 comments on commit 0b76b91

Please sign in to comment.