diff --git a/nodecg-io-shlink/extension/index.ts b/nodecg-io-shlink/extension/index.ts new file mode 100644 index 000000000..34e7a61c9 --- /dev/null +++ b/nodecg-io-shlink/extension/index.ts @@ -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 { + async validateConfig(config: ShlinkServiceConfig): Promise> { + 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> { + const client = new ShlinkClient({ url: config.url, token: config.apiKey }); + return success(client); + } + + stopClient(_client: ShlinkServiceClient): void { + // not needed or possible + } +} diff --git a/nodecg-io-shlink/package.json b/nodecg-io-shlink/package.json new file mode 100644 index 000000000..7c5d7d5d2 --- /dev/null +++ b/nodecg-io-shlink/package.json @@ -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" + } +} diff --git a/nodecg-io-shlink/shlink-schema.json b/nodecg-io-shlink/shlink-schema.json new file mode 100644 index 000000000..cc67c66a8 --- /dev/null +++ b/nodecg-io-shlink/shlink-schema.json @@ -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"] +} diff --git a/nodecg-io-shlink/tsconfig.json b/nodecg-io-shlink/tsconfig.json new file mode 100644 index 000000000..1c8405620 --- /dev/null +++ b/nodecg-io-shlink/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../tsconfig.common.json" +} diff --git a/samples/shlink-list-short-urls/extension/index.ts b/samples/shlink-list-short-urls/extension/index.ts new file mode 100644 index 000000000..0d5d10f2b --- /dev/null +++ b/samples/shlink-list-short-urls/extension/index.ts @@ -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(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.")); +}; diff --git a/samples/shlink-list-short-urls/package.json b/samples/shlink-list-short-urls/package.json new file mode 100644 index 000000000..f1a6921b5 --- /dev/null +++ b/samples/shlink-list-short-urls/package.json @@ -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" + } +} diff --git a/samples/shlink-list-short-urls/tsconfig.json b/samples/shlink-list-short-urls/tsconfig.json new file mode 100644 index 000000000..c8bb01bee --- /dev/null +++ b/samples/shlink-list-short-urls/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig.common.json" +}