| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | import { SNIPPET_TYPE_SCOPES } from '../../types/Snippet' |
| 2 | 2 | import { isNetworkAdmin } from '../screen' |
| 3 | -import type { Snippet, SnippetScope } from '../../types/Snippet' | |
| 3 | +import type { Snippet, SnippetAuthor, SnippetScope } from '../../types/Snippet' | |
| 4 | 4 | |
| 5 | +const TUPLE_SIZE = 2 | |
| 6 | + | |
| 5 | 7 | const defaults: Omit<Snippet, 'tags'> = { |
| 6 | 8 | id: 0, |
| 7 | 9 | name: '', |
| 8 | 10 | code: '', |
| @@ -9,12 +11,16 @@ | ||
| 9 | 11 | desc: '', |
| 10 | 12 | scope: 'global', |
| 11 | 13 | modified: '', |
| 12 | 14 | active: false, |
| 15 | + locked: false, | |
| 16 | + trashed: false, | |
| 13 | 17 | network: isNetworkAdmin(), |
| 14 | 18 | shared_network: null, |
| 15 | 19 | priority: 10, |
| 16 | - conditionId: 0 | |
| 20 | + conditionId: 0, | |
| 21 | + code_error: null, | |
| 22 | + code_error_trace: null | |
| 17 | 23 | } |
| 18 | 24 | |
| 19 | 25 | const isAbsInt = (value: unknown): value is number => |
| 20 | 26 | 'number' === typeof value && 0 < value |
| @@ -21,14 +27,31 @@ | ||
| 21 | 27 | |
| 22 | 28 | const parseStringArray = (value: unknown): string[] | undefined => |
| 23 | 29 | Array.isArray(value) ? value.filter(entry => 'string' === typeof entry) : undefined |
| 24 | 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 | + | |
| 25 | 48 | export const isValidScope = (scope: unknown): scope is SnippetScope => |
| 26 | 49 | 'string' === typeof scope && Object.values(SNIPPET_TYPE_SCOPES).some(typeScopes => |
| 27 | 50 | typeScopes.some(typeScope => typeScope === scope)) |
| 28 | 51 | |
| 29 | 52 | export const parseSnippetObject = (fields: unknown): Snippet => { |
| 30 | - const result: { -readonly [F in keyof Snippet]: Snippet[F] } = { ...defaults, tags: [] } | |
| 53 | + const result: Snippet = { ...defaults, tags: [], conditions: {} } | |
| 31 | 54 | |
| 32 | 55 | if ('object' !== typeof fields || null === fields) { |
| 33 | 56 | return result |
| 34 | 57 | } |
| @@ -42,10 +65,19 @@ | ||
| 42 | 65 | ...'tags' in fields && { tags: parseStringArray(fields.tags) ?? result.tags }, |
| 43 | 66 | ...'scope' in fields && isValidScope(fields.scope) && { scope: fields.scope }, |
| 44 | 67 | ...'modified' in fields && 'string' === typeof fields.modified && { modified: fields.modified }, |
| 45 | 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 }, | |
| 46 | 71 | ...'network' in fields && 'boolean' === typeof fields.network && { network: fields.network }, |
| 47 | 72 | ...'shared_network' in fields && 'boolean' === typeof fields.shared_network && { shared_network: fields.shared_network }, |
| 48 | 73 | ...'priority' in fields && 'number' === typeof fields.priority && { priority: fields.priority }, |
| 49 | - ...'condition_id' in fields && isAbsInt(fields.condition_id) && { conditionId: fields.condition_id } | |
| 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) }, | |
| 50 | 82 | } |
| 51 | 83 | } |