| @@ -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 | } |
| @@ -63,6 +76,8 @@ | ||
| 63 | 76 | ...'code_error' in fields && isCodeError(fields.code_error) && { code_error: fields.code_error }, |
| 64 | 77 | ...'code_error_trace' in fields && |
| 65 | 78 | ('string' === typeof fields.code_error_trace || null === fields.code_error_trace) && { code_error_trace: fields.code_error_trace }, |
| 66 | 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) }, | |
| 67 | 82 | } |
| 68 | 83 | } |