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

Commit 89a89d0

Browse files
committed
Migrate typings from nodecg-types to official v2 NodeCG types
1 parent df6c0c6 commit 89a89d0

File tree

199 files changed

+8511
-7419
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+8511
-7419
lines changed

Diff for: .scripts/create-service.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
os.mkdir(f'services/nodecg-io-{service_name}/extension')
4848
with open(f'services/nodecg-io-{service_name}/extension/index.ts', mode='w') as file:
4949
file.writelines([
50-
'import { NodeCG } from "nodecg-types/types/server";\n',
50+
'import NodeCG from "@nodecg/types";\n',
5151
'import { Result, emptySuccess, success, ServiceBundle } from "nodecg-io-core";\n',
5252
f'import {{ {service_name_c}Client }} from "./{service_name_cc}Client";\n',
5353
'\n',
@@ -57,7 +57,7 @@
5757
'\n',
5858
f'export {{ {service_name_c}Client }} from "./{service_name_cc}Client";\n',
5959
'\n',
60-
'module.exports = (nodecg: NodeCG) => {\n',
60+
'module.exports = (nodecg: NodeCG.ServerAPI) => {\n',
6161
f' new {service_name_c}Service(nodecg, "{service_name}", __dirname, "../schema.json").register();\n',
6262
'};\n',
6363
'\n',
@@ -135,11 +135,11 @@
135135
os.mkdir(f'samples/{sample_name}/extension')
136136
with open(f'samples/{sample_name}/extension/index.ts', mode='w') as file:
137137
file.writelines([
138-
'import { NodeCG } from "nodecg-types/types/server";\n',
138+
'import NodeCG from "@nodecg/types";\n',
139139
f'import {{ {service_name_c}Client }} from "nodecg-io-{service_name}";\n',
140140
'import { requireService } from "nodecg-io-core";\n',
141141
'\n',
142-
'module.exports = function (nodecg: NodeCG) {\n',
142+
'module.exports = function (nodecg: NodeCG.ServerAPI) {\n',
143143
f' nodecg.log.info("Sample bundle for {service_name_c} started.");\n',
144144
'\n',
145145
f' const {service_name_cc} = requireService<{service_name_c}Client>(nodecg, "{service_name}");\n',

Diff for: nodecg-io-core/dashboard/authentication.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/// <reference types="nodecg-types/types/browser" />
2-
31
import { updateMonacoLayout } from "./serviceInstance";
42
import { setPassword, isPasswordSet } from "./crypto";
53

@@ -41,7 +39,7 @@ document.addEventListener("DOMContentLoaded", () => {
4139

4240
export async function isLoaded(): Promise<boolean> {
4341
return new Promise((resolve, _reject) => {
44-
nodecg.sendMessage("isLoaded", (_err, res) => resolve(res));
42+
nodecg.sendMessage("isLoaded", (_err, res: boolean) => resolve(res));
4543
setTimeout(() => resolve(false), 5000); // Fallback in case connection gets lost.
4644
});
4745
}

Diff for: nodecg-io-core/dashboard/main.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Define NodeCG instance on global scope
2+
import type { NodeCGAPIClient } from "@nodecg/types/client/api/api.client";
3+
4+
declare global {
5+
const NodeCG: typeof NodeCGAPIClient;
6+
const nodecg: NodeCGAPIClient;
7+
}
8+
19
// Re-export functions that are used in panel.html to the global scope
210
import { loadFramework } from "./authentication";
311
import {

Diff for: nodecg-io-core/dashboard/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"devDependencies": {
2222
"esbuild": "^0.16.12",
2323
"monaco-editor": "^0.36.1",
24-
"nodecg-types": "^1.9.0",
24+
"@nodecg/types": "^2.1.3",
2525
"typescript": "^5.0.3"
2626
},
2727
"dependencies": {

Diff for: nodecg-io-core/extension/bundleManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NodeCG } from "nodecg-types/types/server";
1+
import NodeCG from "@nodecg/types";
22
import { ObjectMap, Service, ServiceDependency, ServiceInstance } from "./service";
33
import { emptySuccess, error, Result } from "./utils/result";
44
import { EventEmitter } from "events";
@@ -11,7 +11,7 @@ export class BundleManager extends EventEmitter {
1111
// Object that maps a bundle name to the array that contains all services that this bundle depends upon
1212
private readonly bundles: ObjectMap<ServiceDependency<unknown>[]> = {};
1313

14-
constructor(private readonly nodecg: NodeCG) {
14+
constructor(private readonly nodecg: NodeCG.ServerAPI) {
1515
super();
1616
}
1717

Diff for: nodecg-io-core/extension/index.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NodeCG } from "nodecg-types/types/server";
1+
import NodeCG from "@nodecg/types";
22
import { ServiceManager } from "./serviceManager";
33
import { BundleManager } from "./bundleManager";
44
import { MessageManager } from "./messageManager";
@@ -7,16 +7,28 @@ import { Service } from "./service";
77
import { PersistenceManager } from "./persistenceManager";
88
import { ServiceProvider } from "./serviceProvider";
99
import { Logger } from "./utils/logger";
10+
11+
/**
12+
* Config schema for the core bundle.
13+
* This is also defined in the configschema.json file.
14+
*/
15+
export interface NodeCGBundleConfig {
16+
automaticLogin?: {
17+
enabled?: boolean;
18+
password?: string;
19+
};
20+
}
21+
1022
/**
1123
* Main type of NodeCG extension that the core bundle exposes.
1224
* Contains references to all internal modules.
1325
*/
1426
export interface NodeCGIOCore {
1527
registerService<R, C>(service: Service<R, C>): void;
16-
requireService<C>(nodecg: NodeCG, serviceType: string): ServiceProvider<C> | undefined;
28+
requireService<C>(nodecg: NodeCG.ServerAPI, serviceType: string): ServiceProvider<C> | undefined;
1729
}
1830

19-
module.exports = (nodecg: NodeCG): NodeCGIOCore => {
31+
module.exports = (nodecg: NodeCG.ServerAPI): NodeCGIOCore => {
2032
nodecg.log.info("Minzig!");
2133

2234
const serviceManager = new ServiceManager(nodecg);
@@ -40,7 +52,7 @@ module.exports = (nodecg: NodeCG): NodeCGIOCore => {
4052
registerService<R, C>(service: Service<R, C>): void {
4153
serviceManager.registerService(service);
4254
},
43-
requireService<C>(nodecg: NodeCG, serviceType: string): ServiceProvider<C> | undefined {
55+
requireService<C>(nodecg: NodeCG.ServerAPI, serviceType: string): ServiceProvider<C> | undefined {
4456
const bundleName = nodecg.bundleName;
4557
const svc = serviceManager.getService(serviceType);
4658

@@ -58,7 +70,7 @@ module.exports = (nodecg: NodeCG): NodeCGIOCore => {
5870
};
5971

6072
function onExit(
61-
nodecg: NodeCG,
73+
nodecg: NodeCG.ServerAPI,
6274
bundleManager: BundleManager,
6375
instanceManager: InstanceManager,
6476
serviceManager: ServiceManager,
@@ -100,7 +112,7 @@ function onExit(
100112
}
101113

102114
function registerExitHandlers(
103-
nodecg: NodeCG,
115+
nodecg: NodeCG.ServerAPI,
104116
bundleManager: BundleManager,
105117
instanceManager: InstanceManager,
106118
serviceManager: ServiceManager,

Diff for: nodecg-io-core/extension/instanceManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NodeCG } from "nodecg-types/types/server";
1+
import NodeCG from "@nodecg/types";
22
import { ObjectMap, Service, ServiceInstance } from "./service";
33
import { emptySuccess, error, Result } from "./utils/result";
44
import { ServiceManager } from "./serviceManager";
@@ -14,7 +14,7 @@ export class InstanceManager extends EventEmitter {
1414
private ajv = new Ajv();
1515

1616
constructor(
17-
private readonly nodecg: NodeCG,
17+
private readonly nodecg: NodeCG.ServerAPI,
1818
private readonly services: ServiceManager,
1919
private readonly bundles: BundleManager,
2020
) {

Diff for: nodecg-io-core/extension/messageManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NodeCG } from "nodecg-types/types/server";
1+
import NodeCG from "@nodecg/types";
22
import { emptySuccess, error, Result, success } from "./utils/result";
33
import { InstanceManager } from "./instanceManager";
44
import { BundleManager } from "./bundleManager";
@@ -35,7 +35,7 @@ export interface SetServiceDependencyMessage extends PasswordMessage {
3535
*/
3636
export class MessageManager {
3737
constructor(
38-
private nodecg: NodeCG,
38+
private nodecg: NodeCG.ServerAPI,
3939
private services: ServiceManager,
4040
private instances: InstanceManager,
4141
private bundles: BundleManager,

Diff for: nodecg-io-core/extension/persistenceManager.ts

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { NodeCG, ReplicantServer } from "nodecg-types/types/server";
1+
import NodeCG from "@nodecg/types";
22
import { InstanceManager } from "./instanceManager";
33
import { BundleManager } from "./bundleManager";
44
import * as crypto from "crypto-js";
55
import { emptySuccess, error, Result, success } from "./utils/result";
66
import { ObjectMap, ServiceDependency, ServiceInstance } from "./service";
77
import { ServiceManager } from "./serviceManager";
8+
import { NodeCGBundleConfig } from ".";
89

910
/**
1011
* Models all the data that needs to be persistent in a plain manner.
@@ -55,10 +56,10 @@ export class PersistenceManager {
5556
private password: string | undefined;
5657
// We store the encrypted data in a replicant, because writing files in a NodeCG bundle isn't very clean
5758
// and the bundle config is read-only. It is only in encrypted form, so it is OK to be accessible in the browser.
58-
private encryptedData: ReplicantServer<EncryptedData>;
59+
private encryptedData: NodeCG.ServerReplicant<EncryptedData>;
5960

6061
constructor(
61-
private readonly nodecg: NodeCG,
62+
private readonly nodecg: NodeCG.ServerAPI<NodeCGBundleConfig>,
6263
private readonly services: ServiceManager,
6364
private readonly instances: InstanceManager,
6465
private readonly bundles: BundleManager,
@@ -94,7 +95,7 @@ export class PersistenceManager {
9495
* If this returns true {{@link load}} will accept any password and use it to encrypt the configuration.
9596
*/
9697
isFirstStartup(): boolean {
97-
return this.encryptedData.value.cipherText === undefined;
98+
return this.encryptedData.value?.cipherText === undefined;
9899
}
99100

100101
/**
@@ -107,7 +108,7 @@ export class PersistenceManager {
107108
return error("Config has already been decrypted and loaded.");
108109
}
109110

110-
if (this.encryptedData.value.cipherText === undefined) {
111+
if (this.encryptedData.value?.cipherText === undefined) {
111112
// No encrypted data has been saved, probably because this is the first startup.
112113
// Therefore nothing needs to be decrypted, and we write an empty config to disk.
113114
this.nodecg.log.info("No saved configuration found, creating a empty one.");
@@ -216,6 +217,10 @@ export class PersistenceManager {
216217

217218
// Encrypt and save data to persistent replicant.
218219
const cipherText = crypto.AES.encrypt(JSON.stringify(data), this.password);
220+
221+
if (this.encryptedData.value === undefined) {
222+
this.encryptedData.value = {};
223+
}
219224
this.encryptedData.value.cipherText = cipherText.toString();
220225
}
221226

@@ -263,7 +268,7 @@ export class PersistenceManager {
263268

264269
// If enabled isn't undefined the JSON schema guarantees that enabled is a boolean and password is a string
265270
const enabled: boolean = this.nodecg.bundleConfig.automaticLogin.enabled;
266-
const password: string = this.nodecg.bundleConfig.automaticLogin.password;
271+
const password: string | undefined = this.nodecg.bundleConfig.automaticLogin.password;
267272

268273
if (enabled === false) {
269274
// We inform the user that automatic login is set up but not activated because having the ability
@@ -274,6 +279,11 @@ export class PersistenceManager {
274279
return;
275280
}
276281

282+
if (password === undefined) {
283+
this.nodecg.log.error("Automatic login is setup but no password is provided.");
284+
return;
285+
}
286+
277287
this.setupAutomaticLogin(password);
278288
}
279289

@@ -289,7 +299,7 @@ export class PersistenceManager {
289299
// So if we want to wait for NodeCG to be loaded we can watch for changes on this replicant and
290300
// if we get a non-empty array it means that NodeCG has finished loading.
291301
this.nodecg.Replicant<unknown[]>("bundles", "nodecg").on("change", async (bundles) => {
292-
if (bundles.length > 0) {
302+
if (bundles && bundles.length > 0) {
293303
try {
294304
this.nodecg.log.info("Attempting to automatically login...");
295305
const loadResult = await this.load(password);
@@ -300,15 +310,15 @@ export class PersistenceManager {
300310
throw loadResult.errorMessage;
301311
}
302312
} catch (err) {
303-
const logMesssage = `Failed to automatically login: ${err}`;
313+
const logMessage = `Failed to automatically login: ${err}`;
304314
if (this.isLoaded()) {
305315
// load() threw an error but nodecg-io is currently loaded nonetheless.
306316
// Anyway, nodecg-io is loaded which is what we wanted
307-
this.nodecg.log.warn(logMesssage);
317+
this.nodecg.log.warn(logMessage);
308318
} else {
309319
// Something went wrong and nodecg-io is not loaded.
310320
// This is a real error, the password might be wrong or some other issue.
311-
this.nodecg.log.error(logMesssage);
321+
this.nodecg.log.error(logMessage);
312322
}
313323
}
314324
}

Diff for: nodecg-io-core/extension/serviceBundle.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NodeCGIOCore } from ".";
2-
import { NodeCG } from "nodecg-types/types/server";
2+
import NodeCG from "@nodecg/types";
33
import { ObjectMap, Service } from "./service";
44
import { Result } from "./utils/result";
55

@@ -17,7 +17,7 @@ import { Logger } from "./utils/logger";
1717
*/
1818
export abstract class ServiceBundle<R, C> implements Service<R, C> {
1919
public core: NodeCGIOCore | undefined;
20-
public nodecg: NodeCG;
20+
public nodecg: NodeCG.ServerAPI;
2121
public serviceType: string;
2222
public schema?: ObjectMap<unknown>;
2323

@@ -39,7 +39,7 @@ export abstract class ServiceBundle<R, C> implements Service<R, C> {
3939
* @param serviceName the name of the service in all-lowercase-and-with-hyphen
4040
* @param pathSegments the path to the schema.json most likely __dirname, "../serviceName-schema.json"
4141
*/
42-
constructor(nodecg: NodeCG, serviceName: string, ...pathSegments: string[]) {
42+
constructor(nodecg: NodeCG.ServerAPI, serviceName: string, ...pathSegments: string[]) {
4343
this.nodecg = nodecg;
4444
this.serviceType = serviceName;
4545
this.schema = this.readSchema(pathSegments);

Diff for: nodecg-io-core/extension/serviceManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Service } from "./service";
2-
import { NodeCG } from "nodecg-types/types/server";
2+
import NodeCG from "@nodecg/types";
33
import { error, Result, success } from "./utils/result";
44

55
/* eslint-disable @typescript-eslint/no-explicit-any */
@@ -10,7 +10,7 @@ import { error, Result, success } from "./utils/result";
1010
export class ServiceManager {
1111
private services: Service<unknown, any>[] = [];
1212

13-
constructor(private readonly nodecg: NodeCG) {}
13+
constructor(private readonly nodecg: NodeCG.ServerAPI) {}
1414

1515
/**
1616
* Registers the passed service which show it in the GUI and allows it to be instanced using {@link createServiceInstance}.

Diff for: nodecg-io-core/extension/serviceProvider.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventEmitter } from "events";
22
import { NodeCGIOCore } from "./index";
3-
import { NodeCG } from "nodecg-types/types/server";
3+
import NodeCG from "@nodecg/types";
44

55
/**
66
* A wrapper around a ServiceClient that has helper functions for setting up callbacks and
@@ -60,12 +60,12 @@ export class ServiceProvider<C> {
6060

6161
/**
6262
* Allows for bundles to require services.
63-
* @param {NodeCG} nodecg the NodeCG instance of your bundle. Is used to get the bundle name of the calling bundle.
63+
* @param {NodeCG.ServerAPI} nodecg the NodeCG instance of your bundle. Is used to get the bundle name of the calling bundle.
6464
* @param {string} serviceType the type of service you want to require, e.g., "twitch" or "spotify".
6565
* @return {ServiceClientWrapper<C> | undefined} a service client wrapper for access to the service client
6666
* or undefined if the core wasn't loaded or the service type doesn't exist.
6767
*/
68-
export function requireService<C>(nodecg: NodeCG, serviceType: string): ServiceProvider<C> | undefined {
68+
export function requireService<C>(nodecg: NodeCG.ServerAPI, serviceType: string): ServiceProvider<C> | undefined {
6969
const core = nodecg.extensions["nodecg-io-core"] as unknown as NodeCGIOCore | undefined;
7070
if (core === undefined) {
7171
nodecg.log.error(

Diff for: nodecg-io-core/extension/utils/logger/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { NodeCG } from "nodecg-types/types/server";
2+
import NodeCG from "@nodecg/types";
33

44
export class Logger {
5-
constructor(private name: string, private nodecg: NodeCG) {}
5+
constructor(private name: string, private nodecg: NodeCG.ServerAPI) {}
66
trace(...args: any[]): void {
77
this.nodecg.log.trace(`[${this.name}] ${args[0]}`, ...args.slice(1));
88
}

Diff for: nodecg-io-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@types/jest": "^28.1.7",
4949
"@types/node": "^18.15.11",
5050
"jest": "^28.1.3",
51-
"nodecg-types": "^1.9.0",
51+
"@nodecg/types": "^2.1.3",
5252
"typescript": "^5.0.3",
5353
"ts-jest": "^28.0.8"
5454
},

0 commit comments

Comments
 (0)