| @@ -1,7 +1,7 @@ | ||
| 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 | 5 | const TUPLE_SIZE = 2 |
| 6 | 6 | |
| 7 | 7 | const defaults: Omit<Snippet, 'tags'> = { |
| @@ -31,14 +31,27 @@ | ||
| 31 | 31 | const isCodeError = (value: unknown): value is readonly [string, number] => |
| 32 | 32 | Array.isArray(value) && TUPLE_SIZE === value.length && |
| 33 | 33 | 'string' === typeof value[0] && 'number' === typeof value[1] |
| 34 | 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 | + | |
| 35 | 48 | export const isValidScope = (scope: unknown): scope is SnippetScope => |
| 36 | 49 | 'string' === typeof scope && Object.values(SNIPPET_TYPE_SCOPES).some(typeScopes => |
| 37 | 50 | typeScopes.some(typeScope => typeScope === scope)) |
| 38 | 51 | |
| 39 | 52 | export const parseSnippetObject = (fields: unknown): Snippet => { |
| 40 | - const result: { -readonly [F in keyof Snippet]: Snippet[F] } = { ...defaults, tags: [] } | |
| 53 | + const result: Snippet = { ...defaults, tags: [], conditions: {} } | |
| 41 | 54 | |
| 42 | 55 | if ('object' !== typeof fields || null === fields) { |
| 43 | 56 | return result |
| 44 | 57 | } |
| @@ -57,11 +70,14 @@ | ||
| 57 | 70 | ...'trashed' in fields && 'boolean' === typeof fields.trashed && { trashed: fields.trashed }, |
| 58 | 71 | ...'network' in fields && 'boolean' === typeof fields.network && { network: fields.network }, |
| 59 | 72 | ...'shared_network' in fields && 'boolean' === typeof fields.shared_network && { shared_network: fields.shared_network }, |
| 60 | 73 | ...'priority' in fields && 'number' === typeof fields.priority && { priority: fields.priority }, |
| 74 | + ...'conditionId' in fields && isAbsInt(fields.conditionId) && { conditionId: fields.conditionId }, | |
| 61 | 75 | ...'condition_id' in fields && isAbsInt(fields.condition_id) && { conditionId: fields.condition_id }, |
| 62 | 76 | ...'code_error' in fields && isCodeError(fields.code_error) && { code_error: fields.code_error }, |
| 63 | - ...'code_error_trace' in fields && ('string' === typeof fields.code_error_trace || null === fields.code_error_trace) && | |
| 64 | - { code_error_trace: fields.code_error_trace }, | |
| 65 | - ...'last_active' in fields && { lastActive: Number(fields.last_active) } | |
| 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) }, | |
| 66 | 82 | } |
| 67 | 83 | } |