All files / shared examples.ts

100% Statements 12/12
100% Branches 12/12
100% Functions 1/1
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124      152x 152x   152x       293x 293x 292x 292x                           152x         152x           152x         152x                                                                                                                                                                
import { z } from "zod";
import { INPUT_LIMITS } from "./input-limits";
 
const OWNER_PATTERN = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
const REPOSITORY_PATTERN = /^[a-z0-9](?:[a-z0-9._-]*[a-z0-9])?$/;
 
export const repositorySlugSchema = z.string()
  .min(3)
  .max(INPUT_LIMITS.examples.maxRepositorySlugChars)
  .refine((value) => {
    const segments = value.split("/");
    if (segments.length !== 2) return false;
    const [owner, repository] = segments;
    return Boolean(
      owner
      && repository
      && owner.length <= INPUT_LIMITS.examples.maxRepositoryOwnerChars
      && repository.length <= INPUT_LIMITS.examples.maxRepositoryNameChars
      && OWNER_PATTERN.test(owner)
      && !owner.includes("--")
      && REPOSITORY_PATTERN.test(repository)
      && repository !== "."
      && repository !== ".."
      && !repository.endsWith(".git"),
    );
  }, "Invalid canonical repository slug");
 
export const examplesRefSchema = z.string()
  .min(1)
  .max(INPUT_LIMITS.examples.maxRefChars)
  .regex(/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/);
 
export const fullCommitShaSchema = z.string().regex(/^[0-9a-f]{40}$/);
 
export type RepositorySlug = z.infer<typeof repositorySlugSchema>;
export type ExamplesRef = z.infer<typeof examplesRefSchema>;
export type FullCommitSha = z.infer<typeof fullCommitShaSchema>;
 
export const browserOverrideSelectionSchema = z.object({
  repository: repositorySlugSchema,
  ref: examplesRefSchema,
}).strict();
 
export const validateExamplesRequestSchema = z.object({
  schemaVersion: z.literal(1),
  selection: browserOverrideSelectionSchema,
}).strict();
 
export type BrowserOverrideSelection = z.infer<typeof browserOverrideSelectionSchema>;
export type ExamplesRequestSelection =
  | { kind: "default" }
  | ({ kind: "browser-override" } & BrowserOverrideSelection);
 
export type ExamplesSourceMetadata =
  | {
      selection: "default";
      mode: "builtin";
      repository: null;
      ref: null;
      revision: null;
      status: "builtin";
      stale: false;
      tutor?: TutorCapabilityMetadata;
    }
  | {
      selection: "default" | "browser-override";
      mode: "repository-ref";
      repository: RepositorySlug;
      ref: ExamplesRef;
      revision: FullCommitSha;
      status: "remote" | "cache";
      stale: boolean;
      tutor?: TutorCapabilityMetadata;
    };
 
export type TutorCapabilityMetadata =
  | { status: "absent" }
  | { status: "valid" }
  | { status: "invalid"; reason: "invalid-tutor-bundle" };
 
export interface ExampleFile {
  name: string;
  path: string;
  content: string;
}
 
export interface ExampleCatalogItem {
  id: string;
  title: string;
  category: string;
  description?: string;
  main: string;
  source: "builtin" | "external";
  files: Array<Omit<ExampleFile, "content">>;
}
 
export interface ExamplesCatalogResponse {
  schemaVersion: 1;
  source: ExamplesSourceMetadata;
  examples: ExampleCatalogItem[];
}
 
export interface ExampleDetailResponse {
  schemaVersion: 1;
  id: string;
  title: string;
  category: string;
  description?: string;
  main: string;
  source: "builtin" | "external";
  revision: FullCommitSha | null;
  files: ExampleFile[];
}
 
export type ExamplesErrorCode =
  | "INVALID_SELECTION"
  | "INVALID_REF"
  | "INVALID_REVISION"
  | "INVALID_SNAPSHOT"
  | "SOURCE_UNAVAILABLE"
  | "EXAMPLE_NOT_FOUND"
  | "RATE_LIMITED"
  | "LOAD_CAPACITY_EXCEEDED";