Skip to content

Commit

Permalink
DEX-413 feat: added SDK references generation code
Browse files Browse the repository at this point in the history
  • Loading branch information
kamal-skyflow committed Nov 15, 2023
1 parent eab834e commit b97f39f
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 4 deletions.
43 changes: 43 additions & 0 deletions markdown-gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require('fs');
const path = require('path');

function getModuleName(filePath) {
const parsedPath = path.parse(filePath);
let moduleName = parsedPath.name;

if (moduleName.endsWith('.default')) {
moduleName = moduleName.slice(0, -8);
}

return moduleName;
}

const directoryPath = path.join(__dirname, './docs/classes');

fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error(`Error reading directory: ${err}`);
return;
}
files.forEach(file => {
if (path.extname(file) === '.md') {
const filePath = path.join(directoryPath, file);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file: ${err}`);
return;
}
const lines = data.split('\n');
lines[0] = `# Class: ${getModuleName(path.parse(file).name)}`;
const updatedContent = lines.join('\n');
fs.writeFile(filePath, updatedContent, err => {
if (err) {
console.error(`Error writing file: ${err}`);
} else {
console.log(`Updated file: ${filePath}`);
}
});
});
}
});
});
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"eslint": "eslint '**/*.js' --max-warnings 0",
"prettier": "prettier --list-different '**/*.{js,ts}'",
"lint": "npm run prettier && npm run eslint",
"lint-fix": "prettier --write '**/*.{js,ts}' && eslint --fix '**/*.js'"
"lint-fix": "prettier --write '**/*.{js,ts}' && eslint --fix '**/*.js'",
"docs-gen": "typedoc && node markdown-gen.js && npx ts-node scripts/processMarkdown.ts"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,7 +42,9 @@
"jsonwebtoken": "^9.0.0",
"jwt-decode": "^2.2.0",
"promise.allsettled": "^1.0.5",
"qs": "^6.10.3"
"qs": "^6.10.3",
"typedoc": "^0.24.4",
"typedoc-plugin-markdown": "^3.15.1"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
Expand Down Expand Up @@ -76,6 +79,6 @@
"null-loader": "^0.1.1",
"nyc": "^15.1.0",
"prettier": "^1.13.7",
"typescript": "^4.4.4"
"typescript": "^4.7.3"
}
}
139 changes: 139 additions & 0 deletions scripts/processMarkdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { readdirSync, readFileSync, statSync, writeFileSync } from "fs";
import path from "path";

function removeListCharacters(markdown: string): string {
// Match list items with bullets, numbered lists, or other list-type characters
// const listRegex = /^(\s*[\*\-\+\•\▸]\s*|\s*\d+\.\s*|\s*\w\.\s*)/;
const listRegex = /^(\s*[\*\-\+\•\▸]\s|^\s*\d+\.\s|^\s*\w\.\s)/;

// Split the Markdown file into lines
const lines = markdown.split('\n');

// Track whether a list is currently in progress
let inList = false;

// Iterate through each line and remove list characters if necessary
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const match = line.match(listRegex);

if (match) {
// Check if the list is continuing or if it's a new list
if (!inList && !lines[i+1].match(listRegex)) {
// Remove list characters
lines[i] = line.replace(listRegex, '');
}
inList = true;
} else {
inList = false;
}
}

// Join the lines back into a single string
const result = lines.join('\n');

return result;
}

function processUrls(markdown: string, itemPath: string): string {
// remove extension .md from urls
const linkRegex = /\[([^\]]+)\]\(([^)]+)\)/g;
const editedMarkdown = markdown.replace(linkRegex, (match, text, url) => {
if(url.split('.').pop().startsWith('md'))
{
const hash = url.split('.').pop().replace('md', '')
let editedUrl = (url.split('.').slice(0, -1)).join('.')+hash;
if(editedUrl.startsWith('../') && editedUrl.split('/').length > 1)
{
editedUrl = `/sdks/skyflow-node/${editedUrl.split('/').slice(1).join('/')}`;
}
else
{
editedUrl = `/sdks/skyflow-node/${itemPath.split('/')[1]}/${editedUrl}`;
}
return `[${text}](${editedUrl})`;
}
return `[${text}](${url})`;
});
return editedMarkdown;
}

function createEnumerationTable(markdown) {
const enumMembersRegex = /### (.+)[\s\S]*?\*\*(.+)\*\* = `(.+)`/g;
const matches = [...markdown.matchAll(enumMembersRegex)];
const lines = markdown.split('\n');

if (matches.length === 0) {
return ''; // No enumeration members found
}

const index = lines.indexOf('## Enumeration Members')

let editedMarkdown = `${lines.slice(0, index+1). join('\n')}\n\n| Member | Value |\n| --- | --- |`;

for (const match of matches) {
const member = match[1];
const value = match[3];
editedMarkdown += `\n| ${member} | ${value} |`
}
editedMarkdown += '\n';
return editedMarkdown;
}


function processMarkdown(markdown: string, isEnum: boolean, itemPath: string): string {
const processLists = removeListCharacters(markdown);

const editUrls = processUrls(processLists, itemPath);

let processedMarkdown = editUrls;

if(isEnum)
{
const processEnums = createEnumerationTable(editUrls);
processedMarkdown = processEnums;
}

return processedMarkdown;
}

// Example usage
// const markdown = `# Class: Skyflow
// ...
// ### init
// * \`Static\` **init**(\`config\`): [\`default\`](Skyflow.default.md)

// * \`Static\` **init**(\`config\`): [\`IGetByIdInput\`](../interfaces/utils_common.IGetByIdInput.md)

// * \`Static\` **init**(\`config\`): [\`IGetByIdInput\`](../interfaces/utils_common.IGetByIdInput.txt)
// * \`Static\` **init**(\`config\`): [\`IGetByIdInput\`](../interfaces/utils_common.IGetByIdInput)
// ...
// `;

function readFolderStructure(folderPath: string, isEnum: boolean) {
const folderContents = readdirSync(folderPath);
folderContents.forEach((item) => {
const itemPath = path.join(folderPath, item);
const isDirectory = statSync(itemPath).isDirectory();

if (isDirectory) {
if(item == 'enums') {
readFolderStructure(itemPath, true);
}
else{
readFolderStructure(itemPath, false);
}
} else {
const markdown = readFileSync(itemPath, 'utf8');
const transformedMarkdown = processMarkdown(markdown, isEnum, itemPath);
const flaggedMarkdown = '{% env enable="nodeJsSdkRef" %}\n\n' + transformedMarkdown + '\n{% /env %}'
writeFileSync(itemPath, flaggedMarkdown, 'utf-8');
}
});
}

const folderPath = './docs/';
readFolderStructure(folderPath, false);
// const transformedMarkdown = processMarkdown(markdown, true);
// console.log(transformedMarkdown);

75 changes: 75 additions & 0 deletions src/vault-api/Skyflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Copyright (c) 2022 Skyflow, Inc.
*/

/**
* @module Skyflow
*/

import Client from './client';
import { printLog } from './utils/logs-helper';
import logs from './utils/logs';
Expand All @@ -25,18 +29,43 @@ import {
} from './utils/common';
import { formatVaultURL } from './utils/helpers';

/**
* Parameters for the Skyflow client.
* @property vaultID ID of the vault to connect to.
* @property vaultURL URL of the vault to connect to.
* @property getBearerToken Function that retrieves a Skyflow bearer token from your backend.
* @property options Additional configuration options.
*/
export interface ISkyflow {
vaultID?: string;
vaultURL?: string;
getBearerToken: () => Promise<string>;
options?: Record<string, any>;
}

/**
* Parent Skyflow class that consists of all the methods exposed to the client.
* @class Skyflow
*/
class Skyflow {
/**
* @internal
*/
#client: Client;

/**
* @internal
*/
#metadata = { };

/**
* @internal
*/
#Controller: Controller;

/**
* @internal
*/
constructor(config: ISkyflow) {
this.#client = new Client(
{
Expand All @@ -50,6 +79,12 @@ class Skyflow {
printLog(logs.infoLogs.BEARER_TOKEN_LISTENER, MessageType.LOG);
}

/**
* Initializes the Skyflow client.
* @public
* @param config Configuration for the Skyflow client.
* @returns Returns an instance of the Skyflow client.
*/
static init(config: ISkyflow): Skyflow {
printLog(logs.infoLogs.INITIALIZE_CLIENT, MessageType.LOG);
config.vaultURL = formatVaultURL(config.vaultURL)
Expand All @@ -58,6 +93,13 @@ class Skyflow {
return skyflow;
}

/**
* Inserts data into the vault.
* @public
* @param records Records to insert.
* @param options Options for the insertion.
* @returns Returns the insert response.
*/
insert(
records: IInsertRecordInput,
options?: IInsertOptions,
Expand All @@ -66,31 +108,64 @@ class Skyflow {
return this.#Controller.insert(records, options);
}

/**
* Returns values that correspond to the specified tokens.
* @public
* @param detokenizeInput Tokens to return values for.
* @returns Tokens to return values for.
*/
detokenize(detokenizeInput: IDetokenizeInput): Promise<IRevealResponseType> {
printLog(logs.infoLogs.DETOKENIZE_TRIGGERED,
MessageType.LOG);
return this.#Controller.detokenize(detokenizeInput);
}

/**
* Returns records by Skyflow ID.
* @public
* @deprecated Use {@link get} instead.
* @param getByIdInput Skyflow IDs.
* @returns Returns the specified records and any errors.
* @public
*/
getById(getByIdInput: IGetByIdInput) {
printLog(logs.infoLogs.GET_BY_ID_TRIGGERED,
MessageType.LOG);
return this.#Controller.getById(getByIdInput);
}

/**
* Returns records by Skyflow IDs or column values.
* @public
* @param getInput Identifiers for the records.
* @returns Returns the specified records and any errors.
*/
get(getInput: IGetInput,options?:IGetOptions) {
printLog(logs.infoLogs.GET_CALL_TRIGGERED,
MessageType.LOG);
return this.#Controller.get(getInput,options);
}

/**
* Invokes a connection to a third-party service.
* @public
* @param config Configuration for the connection.
* @returns Returns the connection response.
*/
invokeConnection(config: IConnectionConfig) {
printLog(logs.infoLogs.INVOKE_CONNECTION_TRIGGERED,
MessageType.LOG);

return this.#Controller.invokeConnection(config);
}

/**
* Updates the configuration of elements inside the composable container.
* @public
* @param updateInput Input data for the update operation.
* @param options Options for the container update.
* @returns Returns the response for the update operation.
*/
update(updateInput: IUpdateInput,options?:IUpdateOptions){
printLog(logs.infoLogs.UPDATE_TRIGGERED,
MessageType.LOG);
Expand Down
Loading

0 comments on commit b97f39f

Please sign in to comment.