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

Commit 5a5afed

Browse files
authored
Merge pull request #61 from codeoverflow-org/bundle-migration-guide
Add guide that explains how you can migrate existing bundles to nodecg-io
2 parents 077f101 + d36feba commit 5a5afed

File tree

5 files changed

+115
-3
lines changed

5 files changed

+115
-3
lines changed

.markdownlint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"MD036": false,
1111
"MD040": false,
1212
"MD041": false,
13-
"MD046": { "style": "fenced" },
13+
"MD046": false,
1414
"MD047": false,
1515
"no-hard-tabs": true,
1616
"whitespace": false
File renamed without changes.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Migrating your existing bundle to nodecg-io
2+
3+
This guide explains you how to migrate an existing nodecg bundle that already meets your requirements to use nodecg-io.
4+
nodecg-io manages service configuration and authentication so your bundle doesn't have to bother with it.
5+
6+
If you are lucky and used the same library as we do for the service you want to migrate, nodecg-io is a drop-in replacement just doing authentication and configuration.
7+
If you have used another library you'll have to do a little more work to migrate to the library that our service uses.
8+
In that case make sure that the migration is worth the effort.
9+
You can use nodecg-io for one service and use your own library of choice for other services if you like to.
10+
11+
## Install `nodecg-io-core`
12+
13+
The first thing you need to do is to install `nodecg-io-core`. You can do this simply using this npm command:
14+
15+
```shell
16+
npm install nodecg-io-core
17+
```
18+
19+
## Import `requireService`
20+
21+
Now that you have `nodecg-io-core` installed in your bundle you can import the `requireService` function that it exposes:
22+
23+
=== "TypeScript/ES Modules"
24+
25+
```typescript
26+
import { requireService } from "nodecg-io-core";
27+
```
28+
29+
=== "CommonJS"
30+
31+
```javascript
32+
const requireService = require("nodecg-io-core").requireService;
33+
```
34+
35+
## Import Service type (TypeScript only)
36+
37+
If you're using TypeScript you'll also need to install the package of the service that you want to use.
38+
You need it to be able to import its type for typesafety.
39+
40+
This example uses `twitch-chat` as an example but you can just replace it with the name of the service you need.
41+
42+
=== "TypeScript"
43+
44+
```shell
45+
npm install nodecg-io-twitch-chat
46+
```
47+
48+
```typescript
49+
import { TwitchChatServiceClient } from "nodecg-io-twitch-chat";
50+
```
51+
52+
Note: You can get the name of the type by using autocomplete in your editor or by looking it up in the service sample.
53+
54+
## Require the service
55+
56+
Now you can finally tell nodecg-io that you want to get access to a service using the `requireService` function.
57+
You'll have to pass your nodecg instance which is used to identify your bundle and the name of the service you want.
58+
In case of TypeScript you'll also need to provide the type of the service.
59+
60+
=== "TypeScript"
61+
62+
```typescript
63+
const twitchChat = requireService<TwitchChatServiceClient>(nodecg, "twitch-chat");
64+
```
65+
66+
=== "JavaScript"
67+
68+
```javascript
69+
const twitchChat = requireService(nodecg, "twitch-chat");
70+
```
71+
72+
## Accessing the service client
73+
74+
### Using `onAvailable` and `onUnavailable` handlers
75+
76+
You can setup handlers that are executed when the user assigns a service instance to your bundle, removes the assignment or when the service client got updated in some way.
77+
78+
Handlers added with `onAvailable` will get called if there was a change and you got a client:
79+
80+
```typescript
81+
// This is the variable with the return value of requireService().
82+
// You may want to change the variable name for the service you are using.
83+
twitchChat.onAvailable(client => {
84+
nodecg.log.info("Client was set");
85+
// You can use the passed client in here to e.g. setup handlers on the client
86+
});
87+
```
88+
89+
`onAvailable` is especially useful to add event handlers to clients.
90+
E.g. if you want to react to donations or chat messages you can add event handlers for these here.
91+
92+
Handlers added with `onUnavailable` will get called if your bundle was not assigned a service instance or if there was an error in service client creation:
93+
94+
```typescript
95+
twitchChat.onUnavailable(() => {
96+
nodecg.log.info("Client was unset");
97+
});
98+
```
99+
100+
### Using `getClient`
101+
102+
Instead of callbacks you can also get access to the client directly:
103+
104+
```typescript
105+
const twitchChatClient = twitchChat.getClient();
106+
```
107+
108+
`getClient` will return the client if your bundle has an assigned service instance that has produced an service client without error
109+
and will return `undefined` otherwise.
110+
This is useful for when you want to use the client due to some external event or from `onAvailable` handlers of other services.

docs/getting_started/install.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ If you want to every change your nodecg-io installation to add/remove a service
8383

8484
When starting nodecg you should see that all nodecg-io related bundles should be loaded and that you can you can now access nodecg-io in your nodecg dashboard.
8585

86-
Now you can either install a already existing bundle that uses nodecg-io or [create a new bundle](./create_bundle.md).
86+
Now you can either install a already existing bundle that uses nodecg-io, [create a new bundle](./create_new_bundle.md) or [integrate an existing bundle](./existing_bundle.md).
8787

8888
## Uninstall nodecg-io
8989

mkdocs.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ markdown_extensions:
3030
pygments_style: monokai
3131
- pymdownx.inlinehilite
3232
- pymdownx.superfences
33+
- pymdownx.tabbed
3334
- pymdownx.tasklist:
3435
custom_checkbox: true
3536

3637
nav:
3738
- Home: index.md
3839
- Getting Started:
3940
- Installation: getting_started/install.md
40-
- Your first bundle: getting_started/create_bundle.md
41+
- Your first bundle: getting_started/create_new_bundle.md
42+
- Migrating an existing bundle: getting_started/existing_bundle.md
4143
- Configuration: getting_started/configuration.md
4244
- Contribute:
4345
- Basics: contribute/contribute.md

0 commit comments

Comments
 (0)