Skip to content

Commit

Permalink
chore: add missing files/code to exercise web vector examples in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
cprice404 committed Oct 23, 2023
1 parent a75224a commit 08df093
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 35 deletions.
3 changes: 3 additions & 0 deletions examples/web/vector-index/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
**/*.d.ts
62 changes: 62 additions & 0 deletions examples/web/vector-index/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"root": true,
"env": {
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:import/recommended",
"plugin:prettier/recommended",
"plugin:node/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint"],
"rules": {
"semi": ["error", "always"],
"import/no-extraneous-dependencies": ["error", {}],
"node/no-unsupported-features/es-syntax": "off",
"node/no-missing-import": [
"error",
{
"tryExtensions": [".js", ".ts", ".json", ".node"]
}
],
"prettier/prettier": "error",
"block-scoped-var": "error",
"eqeqeq": "error",
"no-var": "error",
"prefer-const": "error",
"eol-last": "error",
"prefer-arrow-callback": "error",
"no-trailing-spaces": "error",
"quotes": ["warn", "single", {"avoidEscape": true}],
"no-restricted-properties": [
"error",
{
"object": "describe",
"property": "only"
},
{
"object": "it",
"property": "only"
}
],
// async without await is often an error and in other uses it obfuscates
// the intent of the developer. Functions are async when they want to await.
"require-await": "error",
"import/no-duplicates": "error"
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
}
}
8 changes: 8 additions & 0 deletions examples/web/vector-index/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"bracketSpacing": false,
"singleQuote": true,
"trailingComma": "es5",
"arrowParens": "avoid",
"printWidth": 120
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import {
CreateVectorIndex,
DeleteVectorIndex,
ListVectorIndexes,
PreviewVectorIndexClient, VectorSearch,
VectorUpsertItemBatch
} from "@gomomento/sdk-web";
import {ALL_VECTOR_METADATA} from "@gomomento/sdk-core";
PreviewVectorIndexClient,
VectorSearch,
VectorUpsertItemBatch,
ALL_VECTOR_METADATA,
Configurations,
CredentialProvider,
} from '@gomomento/sdk-web';
import {initJSDom} from '../utils/jsdom';

async function example_API_CreateIndex(vectorClient: PreviewVectorIndexClient) {
const result = await vectorClient.createIndex('test-index', 2);
Expand All @@ -23,18 +27,14 @@ async function example_API_CreateIndex(vectorClient: PreviewVectorIndexClient) {
async function example_API_ListIndexes(vectorClient: PreviewVectorIndexClient) {
const result = await vectorClient.listIndexes();
if (result instanceof ListVectorIndexes.Success) {
console.log(
`Indexes:\n${result
.getIndexNames()
.join('\n')}\n\n`
);
console.log(`Indexes:\n${result.getIndexNames().join('\n')}\n\n`);
} else if (result instanceof ListVectorIndexes.Error) {
throw new Error(`An error occurred while attempting to list caches: ${result.errorCode()}: ${result.toString()}`);
}
}

async function example_API_DeleteIndex(vectorClient: PreviewVectorIndexClient) {
const result = await vectorClient.deleteIndex('test-index')
const result = await vectorClient.deleteIndex('test-index');
if (result instanceof DeleteVectorIndex.Success) {
console.log("Index 'test-index' deleted");
} else if (result instanceof DeleteVectorIndex.Error) {
Expand Down Expand Up @@ -81,3 +81,23 @@ async function example_API_Search(vectorClient: PreviewVectorIndexClient) {
throw new Error(`An error occurred searching index test-index: ${result.errorCode()}: ${result.toString()}`);
}
}

async function main() {
// Because the Momento Web SDK is intended for use in a browser, we use the JSDom library to set up an environment
// that will allow us to use it in a node.js program.
initJSDom();
const vectorClient = new PreviewVectorIndexClient({
configuration: Configurations.Laptop.latest(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({environmentVariableName: 'MOMENTO_API_KEY'}),
});
await example_API_CreateIndex(vectorClient);
await example_API_ListIndexes(vectorClient);
await example_API_UpsertItemBatch(vectorClient);
await example_API_Search(vectorClient);
await example_API_DeleteItemBatch(vectorClient);
await example_API_DeleteIndex(vectorClient);
}

main().catch(e => {
throw e;
});
46 changes: 23 additions & 23 deletions examples/web/vector-index/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/web/vector-index/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"prebuild": "eslint . --ext .ts",
"build": "tsc",
"validate-examples": "tsc && node dist/doc-examples-web-apis.js",
"validate-examples": "tsc && node dist/doc-example-files/doc-examples-web-apis.js",
"test": "jest",
"lint": "eslint . --ext .ts",
"format": "eslint . --ext .ts --fix"
Expand All @@ -26,7 +26,7 @@
"typescript": "4.4.3"
},
"dependencies": {
"@gomomento/sdk-web": "^1.41.0",
"@gomomento/sdk-web": "^1.45.0",
"jsdom": "22.1.0"
}
}
27 changes: 27 additions & 0 deletions examples/web/vector-index/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "ES2021",
"module": "commonjs",
"lib": [
"es2021",
],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": [
"./node_modules/@types"
],
"outDir": "./dist"
}
}
61 changes: 61 additions & 0 deletions examples/web/vector-index/utils/jsdom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable @typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck

const JSDOM = require('jsdom');

const defaultHtml = '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';

export function initJSDom(html = defaultHtml, options = {}) {
// Idempotency
if (
global.navigator &&
global.navigator.userAgent &&
global.navigator.userAgent.includes('Node.js') &&
global.document &&
typeof global.document.destroy === 'function'
) {
return global.document.destroy;
}

if (!('url' in options)) {
Object.assign(options, {url: 'http://localhost:3000'});
}

// enable pretendToBeVisual by default since react needs
// window.requestAnimationFrame, see https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
if (!('pretendToBeVisual' in options)) {
Object.assign(options, {pretendToBeVisual: true});
}

const jsdom = new JSDOM.JSDOM(html, options);
const {window} = jsdom;
const {document} = window;

// generate our list of keys by enumerating document.window - this list may vary
// based on the jsdom version. filter out internal methods as well as anything
// that node already defines

const KEYS = [];

if (KEYS.length === 0) {
KEYS.push(...Object.getOwnPropertyNames(window).filter(k => !k.startsWith('_') && !(k in global)));
// going to add our jsdom instance, see below
KEYS.push('$jsdom');
}
KEYS.forEach(key => (global[key] = window[key]));

// setup document / window / window.console
global.document = document;
global.window = window;
window.console = global.console;

// add access to our jsdom instance
global.$jsdom = jsdom;

const cleanup = () => KEYS.forEach(key => delete global[key]);

document.destroy = cleanup;

return cleanup;
}

0 comments on commit 08df093

Please sign in to comment.