@teamkeel/wasm 0.314.0 → 0.315.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -11,6 +11,29 @@ export function completions(
11
11
  configFile: string
12
12
  ): Promise<CompletionResult>;
13
13
 
14
+ export function getDefinition(
15
+ req: GetDefinitionRequest
16
+ ): Promise<DefinitionResult>;
17
+
18
+ export interface DefinitionResult {
19
+ schema?: Position;
20
+ function?: { name: string };
21
+ }
22
+
23
+ export interface SchemaDefinition {
24
+ schema: SchemaDefinition;
25
+ }
26
+
27
+ export interface GetDefinitionRequest {
28
+ position: Position;
29
+ schemaFiles: SchemaFile[];
30
+ }
31
+
32
+ export interface SchemaFile {
33
+ filename: string;
34
+ contents: string;
35
+ }
36
+
14
37
  export interface SimplePosition {
15
38
  column: number;
16
39
  line: number;
package/index.js CHANGED
@@ -30,8 +30,14 @@ async function completions() {
30
30
  return api.completions(...arguments);
31
31
  }
32
32
 
33
+ async function getDefinition() {
34
+ const api = await keel();
35
+ return api.getDefinition(...arguments);
36
+ }
37
+
33
38
  module.exports = {
34
39
  format,
35
40
  validate,
36
41
  completions,
42
+ getDefinition,
37
43
  };
package/index.test.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { format, validate, completions } from "./index";
1
+ import { format, validate, completions, getDefinition } from "./index";
2
2
  import { test, expect } from "vitest";
3
3
 
4
4
  const configFile = `
@@ -71,3 +71,63 @@ test("validate - invalid schema", async () => {
71
71
  expect(errors[0].code).toEqual("E025");
72
72
  expect(errors[0].message).toEqual(` unexpected token "<EOF>" (expected "}")`);
73
73
  });
74
+
75
+ test("getDefinition", async () => {
76
+ const result = await getDefinition({
77
+ position: {
78
+ line: 7,
79
+ column: 21,
80
+ offset: 0,
81
+ filename: "myschema.keel",
82
+ },
83
+ schemaFiles: [
84
+ {
85
+ filename: "myschema.keel",
86
+ contents: `
87
+ model Person {
88
+ fields {
89
+ name Text
90
+ }
91
+ operations {
92
+ list getPeople(name)
93
+ }
94
+ }
95
+ `,
96
+ },
97
+ ],
98
+ });
99
+
100
+ expect(result).toEqual({
101
+ function: null,
102
+ schema: {
103
+ filename: "myschema.keel",
104
+ line: 4,
105
+ column: 5,
106
+ },
107
+ });
108
+ });
109
+
110
+ test("getDefinition - no result", async () => {
111
+ const result = await getDefinition({
112
+ position: {
113
+ line: 1,
114
+ column: 1,
115
+ offset: 0,
116
+ filename: "myschema.keel",
117
+ },
118
+ schemaFiles: [
119
+ {
120
+ filename: "myschema.keel",
121
+ contents: `
122
+ model Person {
123
+ fields {
124
+ name Text
125
+ }
126
+ }
127
+ `,
128
+ },
129
+ ],
130
+ });
131
+
132
+ expect(result).toBeNull();
133
+ });
package/lib/main.go CHANGED
@@ -9,6 +9,7 @@ import (
9
9
  "github.com/teamkeel/keel/config"
10
10
  "github.com/teamkeel/keel/schema"
11
11
  "github.com/teamkeel/keel/schema/completions"
12
+ "github.com/teamkeel/keel/schema/definitions"
12
13
  "github.com/teamkeel/keel/schema/format"
13
14
  "github.com/teamkeel/keel/schema/node"
14
15
  "github.com/teamkeel/keel/schema/parser"
@@ -20,9 +21,10 @@ func init() {
20
21
  // we have to declare our functions in an init func otherwise they aren't
21
22
  // available in JS land at the call time.
22
23
  js.Global().Set("keel", js.ValueOf(map[string]any{
23
- "validate": js.FuncOf(validate),
24
- "format": js.FuncOf(formatSchema),
25
- "completions": js.FuncOf(provideCompletions),
24
+ "validate": js.FuncOf(validate),
25
+ "format": js.FuncOf(formatSchema),
26
+ "completions": js.FuncOf(provideCompletions),
27
+ "getDefinition": js.FuncOf(getDefinition),
26
28
  }))
27
29
  }
28
30
 
@@ -94,6 +96,49 @@ func provideCompletions(this js.Value, args []js.Value) any {
94
96
  })
95
97
  }
96
98
 
99
+ // Expected argument to definitions API:
100
+ //
101
+ // {
102
+ // position: {
103
+ // filename: "",
104
+ // line: 1,
105
+ // column: 1,
106
+ // },
107
+ // schemaFiles: [
108
+ // {
109
+ // filename: "",
110
+ // contents: "",
111
+ // },
112
+ // ],
113
+ // }
114
+ func getDefinition(this js.Value, args []js.Value) any {
115
+ return newPromise(func() (any, error) {
116
+ positionArg := args[0].Get("position")
117
+ pos := definitions.Position{
118
+ Filename: positionArg.Get("filename").String(),
119
+ Line: positionArg.Get("line").Int(),
120
+ Column: positionArg.Get("column").Int(),
121
+ }
122
+
123
+ schemaFilesArg := args[0].Get("schemaFiles")
124
+ schemaFiles := []*reader.SchemaFile{}
125
+ for i := 0; i < schemaFilesArg.Length(); i++ {
126
+ f := schemaFilesArg.Index(i)
127
+ schemaFiles = append(schemaFiles, &reader.SchemaFile{
128
+ FileName: f.Get("filename").String(),
129
+ Contents: f.Get("contents").String(),
130
+ })
131
+ }
132
+
133
+ def := definitions.GetDefinition(schemaFiles, pos)
134
+ if def == nil {
135
+ return nil, nil
136
+ }
137
+
138
+ return toMap(def)
139
+ })
140
+ }
141
+
97
142
  func formatSchema(this js.Value, args []js.Value) any {
98
143
  return newPromise(func() (any, error) {
99
144
  src := args[0].String()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamkeel/wasm",
3
- "version": "0.314.0",
3
+ "version": "0.315.0",
4
4
  "description": "A wrapper around the Keel WASM binary",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",