All files / src/sections api-reference.ts

100% Statements 114/114
100% Branches 22/22
100% Functions 10/10
100% Lines 114/114

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 1151x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 1x 1x 5x 5x 5x 5x 5x 5x 5x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 1x 3x 1x 1x 1x 1x 2x 3x 7x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 7x 2x 2x 2x 2x 2x 2x 7x 7x 6x 6x 6x 2x 2x 6x 6x 2x 2x 6x 6x 6x 7x 1x 1x 7x 7x  
import { DocumentSection } from './section.js';
import { heading } from '../elements/heading.js';
 
interface Parameter {
  name: string;
  type: string;
  description: string;
}
 
export interface ApiEndpoint {
  title: string;
  httpMethod: string;
  path: string;
  parameter: Parameter;
}
 
export interface ApiFunction {
  name: string;
  parameters: Array<string>;
  description: string;
}
 
const isObject = (maybeObject: unknown): maybeObject is Object => {
  return (
    typeof maybeObject === 'object' &&
    !Array.isArray(maybeObject) &&
    maybeObject !== null
  );
};
 
const isApiEndpoint = (reference: unknown): reference is ApiEndpoint => {
  return (
    isObject(reference) &&
    ['title', 'httpMethod', 'path', 'parameter'].every(
      (property) => property in reference,
    )
  );
};
 
const isApiFunction = (reference: unknown): reference is ApiFunction => {
  return (
    isObject(reference) &&
    ['name', 'parameters', 'description'].every(
      (property) => property in reference,
    )
  );
};
 
export interface ApiReferenceOptions {
  title?: string;
  items?: Array<ApiEndpoint | ApiFunction>;
}
 
export class ApiReference extends DocumentSection {
  private _apiEndpoints: Array<ApiEndpoint>;
  private _apiFunctions: Array<ApiFunction>;
 
  constructor({ title, items }: ApiReferenceOptions = {}) {
    super({ title: title ?? 'API Reference' });
    this._apiEndpoints = items?.filter(isApiEndpoint) ?? [];
    this._apiFunctions = items?.filter(isApiFunction) ?? [];
  }
 
  public add(reference: ApiEndpoint | ApiFunction) {
    if (isApiEndpoint(reference)) {
      this._apiEndpoints.push(reference);
    } else if (isApiFunction(reference)) {
      this._apiFunctions.push(reference);
    } else {
      throw new Error('Unknown Reference Type');
    }
    return this;
  }
 
  private mapEndpoint(apiEndpoint: ApiEndpoint): string {
    return [
      heading(4, apiEndpoint.title),
      '',
      '```http',
      'GET /api/items',
      '```',
      '',
      '| Parameter | Type     | Description                |',
      '| :-------- | :------- | :------------------------- |',
      `|${apiEndpoint.parameter.name} | ${apiEndpoint.parameter.type} | ${apiEndpoint.parameter.description}|`,
    ].join('\n');
  }
 
  private mapFunction(apiFunction: ApiFunction): string {
    return [
      heading(4, `${apiFunction.name}(${apiFunction.parameters.join(', ')})`),
      '',
      apiFunction.description,
    ].join('\n');
  }
 
  protected synthesizeContent(): string[] {
    const content = [heading(2, this.title)];
 
    if (this._apiEndpoints.length) {
      content.push('', this._apiEndpoints.map(this.mapEndpoint).join('\n\n'));
    }
 
    if (this._apiFunctions.length) {
      content.push('', this._apiFunctions.map(this.mapFunction).join('\n\n'));
    }
 
    return content;
  }
}
 
export function apiReferenceSection(options?: ApiReferenceOptions) {
  return new ApiReference(options);
}