An open API service indexing awesome lists of open source software.

https://github.com/orta/md-type-tables


https://github.com/orta/md-type-tables

Last synced: 4 months ago
JSON representation

Awesome Lists containing this project

README

          

We take this type:

```ts
type SomeType = {
/**
* Client ID of your GitHub/OAuth App. Find it on your app's settings page.
* @required
*/
clientId: string;
/**
* Client Secret for your GitHub/OAuth App. Create one on your app's settings page.
* @required
*/
clientSecret: string;
/**
* Either "oauth-app" or "github-app". Defaults to "oauth-app".
*/
clientType: string;
};
```

And use it to generate this table:



name


type


description


clientId


string

**Required**
Client ID of your GitHub/OAuth App. Find it on your app's settings page.


clientSecret


string

**Required**
Client Secret for your GitHub/OAuth App. Create one on your app's settings page.


clientType


string

Either "oauth-app" or "github-app". Defaults to "oauth-app".


Using this code:

```js
const ts = require("typescript");

module.exports = {
transforms: {
/* Match */
TYPES(_content, options) {
const mds = [];
let program = ts.createProgram([options.src], {});
const sourceFile = program.getSourceFile(options.src);

/** @type {import("typescript").TypeAliasDeclaration} */
let typeNode = undefined;
const findSymbol = (node) => {
if (
node &&
node.name &&
node.name.escapedText &&
node.name.escapedText === options.symbol
) {
typeNode = node;
}
if (!typeNode) ts.forEachChild(node, findSymbol);
};

findSymbol(sourceFile);
if (!typeNode)
throw new Error(`Could not find ${options.symbol} in ${options.src}`);

mds.push(prefix);
typeNode.type.members.forEach((member) => {
const name = member.name.escapedText;
const type = sourceFile.text.slice(
member.type.pos + 1,
member.type.end
);

const required =
member.jsDoc &&
!!member.jsDoc.find(
(d) => d.tags && d.tags.find((t) => t.tagName.escapedText)
);
const info =
member.jsDoc && member.jsDoc.map((jd) => jd.comment).join("\n\n");

mds.push(`

${name}


${type}

${required ? "**Required**" : ""}
${info}


`);
});

mds.push(suffix);
return mds.join("\n\n");
},
},
};

const prefix = `



name


type


description

`;

const suffix = `


`;
```

You can run it locally:

```sh
git clone https://github.com/orta/md-type-tables
cd md-type-tables
yarn
yarn readme
```