| 1 |
import { SNIPPET_TYPE_SCOPES } from '../../types/Snippet' |
| 2 |
import { isNetworkAdmin } from '../screen' |
| 3 |
import type { Snippet, SnippetAuthor, SnippetScope } from '../../types/Snippet' |
| 4 |
|
| 5 |
const TUPLE_SIZE = 2 |
| 6 |
|
| 7 |
const defaults: Omit<Snippet, 'tags'> = { |
| 8 |
id: 0, |
| 9 |
name: '', |
| 10 |
code: '', |
| 11 |
desc: '', |
| 12 |
scope: 'global', |
| 13 |
modified: '', |
| 14 |
active: false, |
| 15 |
locked: false, |
| 16 |
trashed: false, |
| 17 |
network: isNetworkAdmin(), |
| 18 |
shared_network: null, |
| 19 |
priority: 10, |
| 20 |
conditionId: 0, |
| 21 |
code_error: null, |
| 22 |
code_error_trace: null |
| 23 |
} |
| 24 |
|
| 25 |
const isAbsInt = (value: unknown): value is number => |
| 26 |
'number' === typeof value && 0 < value |
| 27 |
|
| 28 |
const parseStringArray = (value: unknown): string[] | undefined => |
| 29 |
Array.isArray(value) ? value.filter(entry => 'string' === typeof entry) : undefined |
| 30 |
|
| 31 |
const isCodeError = (value: unknown): value is readonly [string, number] => |
| 32 |
Array.isArray(value) && TUPLE_SIZE === value.length && |
| 33 |
'string' === typeof value[0] && 'number' === typeof value[1] |
| 34 |
|
| 35 |
const parseSnippetAuthor = (value: unknown): SnippetAuthor | null => { |
| 36 |
if (!value || 'object' !== typeof value) { |
| 37 |
return null |
| 38 |
} |
| 39 |
|
| 40 |
const author = <Record<string, unknown>>value |
| 41 |
return { |
| 42 |
id: Number(author.id ?? 0), |
| 43 |
displayName: 'string' === typeof author.display_name ? author.display_name : '', |
| 44 |
avatarUrl: 'string' === typeof author.avatar_url ? author.avatar_url : '' |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
export const isValidScope = (scope: unknown): scope is SnippetScope => |
| 49 |
'string' === typeof scope && Object.values(SNIPPET_TYPE_SCOPES).some(typeScopes => |
| 50 |
typeScopes.some(typeScope => typeScope === scope)) |
| 51 |
|
| 52 |
export const parseSnippetObject = (fields: unknown): Snippet => { |
| 53 |
const result: Snippet = { ...defaults, tags: [], conditions: {} } |
| 54 |
|
| 55 |
if ('object' !== typeof fields || null === fields) { |
| 56 |
return result |
| 57 |
} |
| 58 |
|
| 59 |
return { |
| 60 |
...result, |
| 61 |
...'id' in fields && isAbsInt(fields.id) && { id: fields.id }, |
| 62 |
...'name' in fields && 'string' === typeof fields.name && { name: fields.name }, |
| 63 |
...'desc' in fields && 'string' === typeof fields.desc && { desc: fields.desc }, |
| 64 |
...'code' in fields && 'string' === typeof fields.code && { code: fields.code }, |
| 65 |
...'tags' in fields && { tags: parseStringArray(fields.tags) ?? result.tags }, |
| 66 |
...'scope' in fields && isValidScope(fields.scope) && { scope: fields.scope }, |
| 67 |
...'modified' in fields && 'string' === typeof fields.modified && { modified: fields.modified }, |
| 68 |
...'active' in fields && 'boolean' === typeof fields.active && { active: fields.active }, |
| 69 |
...'locked' in fields && 'boolean' === typeof fields.locked && { locked: fields.locked }, |
| 70 |
...'trashed' in fields && 'boolean' === typeof fields.trashed && { trashed: fields.trashed }, |
| 71 |
...'network' in fields && 'boolean' === typeof fields.network && { network: fields.network }, |
| 72 |
...'shared_network' in fields && 'boolean' === typeof fields.shared_network && { shared_network: fields.shared_network }, |
| 73 |
...'priority' in fields && 'number' === typeof fields.priority && { priority: fields.priority }, |
| 74 |
...'conditionId' in fields && isAbsInt(fields.conditionId) && { conditionId: fields.conditionId }, |
| 75 |
...'condition_id' in fields && isAbsInt(fields.condition_id) && { conditionId: fields.condition_id }, |
| 76 |
...'code_error' in fields && isCodeError(fields.code_error) && { code_error: fields.code_error }, |
| 77 |
...'code_error_trace' in fields && |
| 78 |
('string' === typeof fields.code_error_trace || null === fields.code_error_trace) && { code_error_trace: fields.code_error_trace }, |
| 79 |
...'last_active' in fields && { lastActive: Number(fields.last_active) }, |
| 80 |
...'created_by' in fields && { createdBy: parseSnippetAuthor(fields.created_by) }, |
| 81 |
...'updated_by' in fields && { updatedBy: parseSnippetAuthor(fields.updated_by) }, |
| 82 |
} |
| 83 |
} |
| 84 |
|