Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Add Shlink service & sample bundle #260

Merged
merged 3 commits into from
Oct 13, 2021
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
32 changes: 32 additions & 0 deletions nodecg-io-shlink/extension/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NodeCG } from "nodecg-types/types/server";
import { Result, emptySuccess, success, ServiceBundle } from "nodecg-io-core";
import { ShlinkClient } from "shlink-client";

interface ShlinkServiceConfig {
url: string;
apiKey: string;
}

export type ShlinkServiceClient = ShlinkClient;

module.exports = (nodecg: NodeCG) => {
new ShlinkService(nodecg, "shlink", __dirname, "../shlink-schema.json").register();
};

class ShlinkService extends ServiceBundle<ShlinkServiceConfig, ShlinkServiceClient> {
async validateConfig(config: ShlinkServiceConfig): Promise<Result<void>> {
const client = new ShlinkClient({ url: config.url, token: config.apiKey });

await client.countVisits(); // will throw a meaningful error if something went wrong
return emptySuccess();
}

async createClient(config: ShlinkServiceConfig): Promise<Result<ShlinkServiceClient>> {
const client = new ShlinkClient({ url: config.url, token: config.apiKey });
return success(client);
}

stopClient(_client: ShlinkServiceClient): void {
// not needed or possible
}
}
47 changes: 47 additions & 0 deletions nodecg-io-shlink/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "nodecg-io-shlink",
"version": "0.2.0",
"description": "Allows you to create Shlink short links",
"homepage": "https://nodecg.io/RELEASE/samples/shlink",
"author": {
"name": "voruti",
"url": "https://github.com/voruti"
},
"repository": {
"type": "git",
"url": "https://github.com/codeoverflow-org/nodecg-io.git",
"directory": "nodecg-io-shlink"
},
"files": [
"**/*.js",
"**/*.js.map",
"**/*.d.ts",
"*.json"
],
"main": "extension/index",
"scripts": {
"build": "tsc -b",
"watch": "tsc -b -w",
"clean": "tsc -b --clean"
},
"keywords": [
"nodecg-io",
"nodecg-bundle"
],
"nodecg": {
"compatibleRange": "^1.1.1",
"bundleDependencies": {
"nodecg-io-core": "^0.2.0"
}
},
"license": "MIT",
"devDependencies": {
"@types/node": "^15.0.2",
"nodecg-types": "^1.8.2",
"typescript": "^4.2.4"
},
"dependencies": {
"nodecg-io-core": "^0.2.0",
"shlink-client": "^1.0.1"
}
}
16 changes: 16 additions & 0 deletions nodecg-io-shlink/shlink-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"url": {
"type": "string",
"description": "The url to the Shlink server"
},
"apiKey": {
"type": "string",
"description": "The API key of the Shlink server"
}
},
"required": ["url", "apiKey"]
}
3 changes: 3 additions & 0 deletions nodecg-io-shlink/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.common.json"
}
21 changes: 21 additions & 0 deletions samples/shlink-list-short-urls/extension/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NodeCG } from "nodecg-types/types/server";
import { requireService } from "nodecg-io-core";
import { ShlinkServiceClient } from "nodecg-io-shlink";

module.exports = function (nodecg: NodeCG) {
nodecg.log.info("Sample bundle for Shlink started");

const service = requireService<ShlinkServiceClient>(nodecg, "shlink");

service?.onAvailable((client) => {
nodecg.log.info("Shlink client has been updated.");

client.getShortUrls().then((response) => {
nodecg.log.info(
`Received ${response.pagination.totalItems} short urls from Shlink server (on first page).`,
);
});
});

service?.onUnavailable(() => nodecg.log.info("Shlink client has been unset."));
};
24 changes: 24 additions & 0 deletions samples/shlink-list-short-urls/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "shlink-list-short-urls",
"version": "0.2.0",
"private": true,
"nodecg": {
"compatibleRange": "^1.1.1",
"bundleDependencies": {
"nodecg-io-shlink": "^0.2.0"
}
},
"scripts": {
"build": "tsc -b",
"watch": "tsc -b -w",
"clean": "tsc -b --clean"
},
"license": "MIT",
"dependencies": {
"@types/node": "^15.0.2",
"nodecg-types": "^1.8.2",
"nodecg-io-core": "^0.2.0",
"nodecg-io-shlink": "^0.2.0",
"typescript": "^4.2.4"
}
}
3 changes: 3 additions & 0 deletions samples/shlink-list-short-urls/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.common.json"
}