arnoudkooi / sn-scriptsync

Use sn-scriptsync in combination with SN Utils to be able to code in VS Code, including all benefits of a full fledged code editor
https://marketplace.visualstudio.com/items?itemName=arnoudkooicom.sn-scriptsync
Other
43 stars 28 forks source link

Feature Request: Pull all property names from instance as autocomplete options for gs.getProperties #77

Closed VanWeapon closed 1 year ago

VanWeapon commented 1 year ago

Typescript types allow for declaration of a set of constant strings like so:

//in a .d.ts file
declare type Properties = "com.snc.someprop" | "glide.ui.something" | "custom.property.name_thing"; //etc etc

image

Then you can update the definition of gs.getProperty in the d.ts file like below

    getProperty(key: Properties, alt: Object): string {}

image

Then, when you try to use gs.getProperty in a script, you actually get autocomplete showing you only valid property names.

image

Since this list is large, this should probably only be pulled/refreshed on demand. Maybe once optionally during initial instance setup, then again as a separate command

For now, I can load this list manually by exporting to csv or something then loading into my own typedef file, but it would be cool if the extension could do this too. Maybe a similar thing could be done for table names so that GlideRecord can only be used on valid tables?

VanWeapon commented 1 year ago

here is some code i wrote to do this on-demand in node for table names, tho the concept applies broadly:

var fs = require('fs');
const axios = require('axios');

async function getTableRest() {
    let url = 'https://instance.service-now.com/api/now/table/sys_db_object?sysparm_fields=name';
    let auth = 'Basic ' + btoa('username' + ':' + 'password');
    axios
        .get(url, {
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
                Authorization: auth,
            },
        })
        .then((res) => {
            // console.log(res.data.result);
            // console.log(res);
            let results = res.data.result;
            // console.log(results);

            fs.writeFileSync('./ts/tablenames.d.ts', `declare type InstanceTableNames = `);
            for (var x of results) {
                var table_name = x.name;
                if (!table_name) continue;
                fs.appendFile(
                    './ts/tablenames.d.ts',
                    `
    | "${table_name}"`,
                    { encoding: 'utf-8' },
                    () => {}
                );
            }
        });
}

getTableRest();

Resulting file: image

Then I just change the type signature of GlideRecord (and GlideAggregate) image

and now i have autocomplete for table names in GlideRecord:

image

arnoudkooi commented 1 year ago

Interesting didn't know this! Does this result in an error when the used value is not in the type, or preloaded? (that may be confusing for the average user)

VanWeapon commented 1 year ago

No errors, since these aren't TypeScript files they don't get checked unless the user runs a TSLint explicitly

arnoudkooi commented 1 year ago

Implemented :)