mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-21 06:59:09 +00:00
small strict improvements (#1428)
* small `strict` improvements * fix type * use isObject
This commit is contained in:
committed by
GitHub
parent
743d7fa782
commit
cd913fc144
@@ -51,7 +51,7 @@ export function generateRundownPreview(options: ImportMap): { rundown: OntimeRun
|
|||||||
}
|
}
|
||||||
|
|
||||||
// clear the data
|
// clear the data
|
||||||
excelData = undefined;
|
excelData = xlsx.utils.book_new();
|
||||||
|
|
||||||
return { rundown, customFields };
|
return { rundown, customFields };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,10 +74,11 @@ export function revoke(): ReturnType<typeof hasAuth> {
|
|||||||
*/
|
*/
|
||||||
export function handleClientSecret(clientSecret: string): ClientSecret {
|
export function handleClientSecret(clientSecret: string): ClientSecret {
|
||||||
const clientSecretObject = JSON.parse(clientSecret);
|
const clientSecretObject = JSON.parse(clientSecret);
|
||||||
const isValid = validateClientSecret(clientSecretObject);
|
|
||||||
|
|
||||||
if (!isValid) {
|
try {
|
||||||
throw new Error('Client secret invalid');
|
validateClientSecret(clientSecretObject);
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Client secret is invalid: ${error}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return clientSecretObject;
|
return clientSecretObject;
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { isOntimeBlock, isOntimeEvent, OntimeRundownEntry } from 'ontime-types';
|
import { isOntimeBlock, isOntimeEvent, OntimeEvent, OntimeRundownEntry } from 'ontime-types';
|
||||||
import { millisToString } from 'ontime-utils';
|
import { millisToString } from 'ontime-utils';
|
||||||
|
|
||||||
import { sheets_v4 } from '@googleapis/sheets';
|
import { sheets_v4 } from '@googleapis/sheets';
|
||||||
|
import { isObject } from '../../utils/assert.js';
|
||||||
|
|
||||||
// we expect client secret file to contain the following keys
|
// we expect client secret file to contain the following keys
|
||||||
const requiredClientKeys = [
|
const requiredClientKeys = [
|
||||||
@@ -26,10 +27,21 @@ export type ClientSecret = {
|
|||||||
/**
|
/**
|
||||||
* Guard validates a given client secrets file
|
* Guard validates a given client secrets file
|
||||||
* @param clientSecret
|
* @param clientSecret
|
||||||
* @returns
|
* @throws
|
||||||
*/
|
*/
|
||||||
export function validateClientSecret(clientSecret: object): clientSecret is ClientSecret {
|
export function validateClientSecret(clientSecret: object): clientSecret is ClientSecret {
|
||||||
return requiredClientKeys.every((key) => Object.keys(clientSecret['installed']).includes(key));
|
if (!('installed' in clientSecret)) {
|
||||||
|
throw new Error('Missing "installed" object');
|
||||||
|
}
|
||||||
|
|
||||||
|
const { installed } = clientSecret;
|
||||||
|
isObject(installed);
|
||||||
|
|
||||||
|
if (requiredClientKeys.every((key) => Object.keys(installed).includes(key))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Missing keys in "installed" object');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,18 +77,18 @@ export function getA1Notation(row: number, column: number): string {
|
|||||||
* @param {OntimeRundownEntry} event
|
* @param {OntimeRundownEntry} event
|
||||||
* @param {number} index - index of the event
|
* @param {number} index - index of the event
|
||||||
* @param {number} worksheetId
|
* @param {number} worksheetId
|
||||||
* @param {any} metadata - object with all the cell positions of the title of each attribute
|
* @param {object} metadata - object with all the cell positions of the title of each attribute
|
||||||
* @returns {sheets_v4.Schema} - list of update requests
|
* @returns {sheets_v4.Schema} - list of update requests
|
||||||
*/
|
*/
|
||||||
export function cellRequestFromEvent(
|
export function cellRequestFromEvent(
|
||||||
event: OntimeRundownEntry,
|
event: OntimeRundownEntry,
|
||||||
index: number,
|
index: number,
|
||||||
worksheetId: number,
|
worksheetId: number,
|
||||||
metadata,
|
metadata: object,
|
||||||
): sheets_v4.Schema$Request {
|
): sheets_v4.Schema$Request {
|
||||||
const rowData = Object.entries(metadata)
|
const rowData = Object.entries(metadata)
|
||||||
.filter(([_, value]) => value !== undefined)
|
.filter(([_, value]) => value !== undefined)
|
||||||
.sort(([_a, a], [_b, b]) => a['col'] - b['col']) as [string, { col: number; row: number }][];
|
.sort(([_a, a], [_b, b]) => a['col'] - b['col']) as [keyof OntimeEvent | 'blank', { col: number; row: number }][];
|
||||||
|
|
||||||
const titleCol = rowData[0][1].col;
|
const titleCol = rowData[0][1].col;
|
||||||
|
|
||||||
@@ -113,7 +125,7 @@ export function cellRequestFromEvent(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCellData(key: string, event: OntimeRundownEntry) {
|
function getCellData(key: keyof OntimeEvent | 'blank', event: OntimeRundownEntry) {
|
||||||
if (isOntimeEvent(event)) {
|
if (isOntimeEvent(event)) {
|
||||||
if (key === 'blank') {
|
if (key === 'blank') {
|
||||||
return {};
|
return {};
|
||||||
@@ -126,14 +138,13 @@ function getCellData(key: string, event: OntimeRundownEntry) {
|
|||||||
return { userEnteredValue: { stringValue: event.custom[customKey] } };
|
return { userEnteredValue: { stringValue: event.custom[customKey] } };
|
||||||
}
|
}
|
||||||
|
|
||||||
const dataType = typeof event[key];
|
if (typeof event[key] === 'number') {
|
||||||
if (dataType === 'number') {
|
|
||||||
return { userEnteredValue: { stringValue: millisToString(event[key]) } };
|
return { userEnteredValue: { stringValue: millisToString(event[key]) } };
|
||||||
}
|
}
|
||||||
if (dataType === 'string') {
|
if (typeof event[key] === 'string') {
|
||||||
return { userEnteredValue: { stringValue: event[key] } };
|
return { userEnteredValue: { stringValue: event[key] } };
|
||||||
}
|
}
|
||||||
if (dataType === 'boolean') {
|
if (typeof event[key] === 'boolean') {
|
||||||
return { userEnteredValue: { boolValue: event[key] } };
|
return { userEnteredValue: { boolValue: event[key] } };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,19 +15,19 @@ describe('test stringToOSCArgs()', () => {
|
|||||||
|
|
||||||
it('empty is nothing', () => {
|
it('empty is nothing', () => {
|
||||||
const test = undefined;
|
const test = undefined;
|
||||||
const expected = [];
|
const expected: any[] = [];
|
||||||
expect(stringToOSCArgs(test)).toStrictEqual(expected);
|
expect(stringToOSCArgs(test)).toStrictEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('empty is nothing', () => {
|
it('empty is nothing', () => {
|
||||||
const test = '';
|
const test = '';
|
||||||
const expected = [];
|
const expected: any[] = [];
|
||||||
expect(stringToOSCArgs(test)).toStrictEqual(expected);
|
expect(stringToOSCArgs(test)).toStrictEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('1 space is nothing', () => {
|
it('1 space is nothing', () => {
|
||||||
const test = ' ';
|
const test = ' ';
|
||||||
const expected = [];
|
const expected: any[] = [];
|
||||||
expect(stringToOSCArgs(test)).toStrictEqual(expected);
|
expect(stringToOSCArgs(test)).toStrictEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -550,7 +550,7 @@ describe('test aliases import', () => {
|
|||||||
pathAndParams: 'testpathAndParams',
|
pathAndParams: 'testpathAndParams',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
} as DatabaseModel;
|
} as unknown as DatabaseModel;
|
||||||
|
|
||||||
const parsed = parseUrlPresets(testData);
|
const parsed = parseUrlPresets(testData);
|
||||||
expect(parsed.length).toBe(1);
|
expect(parsed.length).toBe(1);
|
||||||
@@ -602,7 +602,7 @@ describe('test views import', () => {
|
|||||||
app: 'ontime',
|
app: 'ontime',
|
||||||
version: '2.0.0',
|
version: '2.0.0',
|
||||||
},
|
},
|
||||||
} as DatabaseModel;
|
} as unknown as DatabaseModel;
|
||||||
const parsed = parseViewSettings(testData);
|
const parsed = parseViewSettings(testData);
|
||||||
expect(parsed).toStrictEqual(dbModel.viewSettings);
|
expect(parsed).toStrictEqual(dbModel.viewSettings);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user