File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed
packages/sql-tag/src/factories Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { FragmentToken } from '../../tokens' ;
2
2
import { createSqlTag } from '../createSqlTag' ;
3
+ import { InvalidInputError } from '@slonik/errors' ;
3
4
import anyTest , { type TestFn } from 'ava' ;
4
5
import { ROARR } from 'roarr' ;
5
6
@@ -17,6 +18,15 @@ test.beforeEach((t) => {
17
18
} ;
18
19
} ) ;
19
20
21
+ test ( 'throws error if called as a function' , ( t ) => {
22
+ const error = t . throws ( ( ) => {
23
+ // @ts -expect-error - intentional
24
+ sql . fragment ( [ `SELECT 1` ] ) ;
25
+ } ) ;
26
+
27
+ t . true ( error instanceof InvalidInputError ) ;
28
+ } ) ;
29
+
20
30
test ( 'creates an object describing a query' , ( t ) => {
21
31
const query = sql . fragment `SELECT 1` ;
22
32
Original file line number Diff line number Diff line change @@ -32,9 +32,15 @@ const log = Logger.child({
32
32
} ) ;
33
33
34
34
const createFragment = (
35
- parts : readonly string [ ] ,
35
+ parts : TemplateStringsArray ,
36
36
values : readonly ValueExpression [ ] ,
37
37
) => {
38
+ if ( ! Array . isArray ( parts . raw ) || ! Object . isFrozen ( parts . raw ) ) {
39
+ throw new InvalidInputError (
40
+ 'Function must be called as a template literal.' ,
41
+ ) ;
42
+ }
43
+
38
44
let rawSql = '' ;
39
45
40
46
const parameterValues : PrimitiveValueExpression [ ] = [ ] ;
@@ -181,7 +187,7 @@ export const createSqlTag = <
181
187
} ,
182
188
type : ( parser ) => {
183
189
return (
184
- parts : readonly string [ ] ,
190
+ parts : TemplateStringsArray ,
185
191
...args : readonly ValueExpression [ ]
186
192
) => {
187
193
return Object . freeze ( {
@@ -199,7 +205,7 @@ export const createSqlTag = <
199
205
}
200
206
201
207
return (
202
- parts : readonly string [ ] ,
208
+ parts : TemplateStringsArray ,
203
209
...args : readonly ValueExpression [ ]
204
210
) => {
205
211
return Object . freeze ( {
You can’t perform that action at this time.
0 commit comments