@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
93 lines • 3.48 kB
JavaScript
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
export const ZERO_TIME = '0001-01-01T00:00:00Z';
export function struct(...fields) {
return { fields, nullable: false };
}
export function pointer(...fields) {
return { fields, nullable: true };
}
export function listOf(shape) {
return { list: shape };
}
export function orNull(shape) {
return { orNull: shape };
}
const REACTION = struct(['content', 'string'], ['users', struct(['totalCount', 'int'])]);
function record(value) {
return value !== null && typeof value === 'object' ? value : {};
}
function text(value) {
return typeof value === 'string' ? value : '';
}
/** gh's `Author.MarshalJSON`: a bot is the one with no id. */
function author(value) {
const row = record(value);
const id = text(row.id);
if (id === '')
return { is_bot: true, login: `app/${text(row.login)}` };
return { id, is_bot: false, login: text(row.login), name: text(row.name) };
}
/** gh's `ReactionGroups.MarshalJSON`: only the groups someone reacted with. */
function reactions(value) {
if (!Array.isArray(value))
return [];
return value
.filter((group) => {
const count = record(record(group).users).totalCount;
return typeof count === 'number' && count > 0;
})
.map((group) => exported(group, REACTION));
}
/** gh's `Owner`, whose id and name are `omitempty`. */
function owner(value) {
const row = record(value);
const out = {};
if (text(row.id) !== '')
out.id = row.id;
if (text(row.name) !== '')
out.name = row.name;
out.login = text(row.login);
return out;
}
/** One value as gh prints it once decoded into `shape`. */
export function exported(value, shape) {
if (shape === 'string')
return typeof value === 'string' ? value : '';
if (shape === 'int')
return typeof value === 'number' ? value : 0;
if (shape === 'bool')
return typeof value === 'boolean' ? value : false;
if (shape === 'time')
return typeof value === 'string' ? value : ZERO_TIME;
if (shape === 'raw')
return value ?? null;
if (shape === 'author')
return author(value);
if (shape === 'reactions')
return reactions(value);
if (shape === 'owner')
return owner(value);
if ('list' in shape) {
return Array.isArray(value) ? value.map((item) => exported(item, shape.list)) : null;
}
if ('orNull' in shape) {
return value === null || value === undefined ? null : exported(value, shape.orNull);
}
if ((value === null || value === undefined) && shape.nullable)
return null;
const row = record(value);
return Object.fromEntries(shape.fields.map(([name, inner, source]) => [name, exported(row[source ?? name], inner)]));
}
//# sourceMappingURL=shape.js.map