Spaces:
Running
Running
Download source/teaql-ts/examples/school-management/request-intent.ts from teaql/browser-sqlite-playground: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/spaces/teaql/browser-sqlite-playground/resolve/main/source/teaql-ts/examples/school-management/request-intent.ts
- Command line
-
hf download hf://spaces/teaql/browser-sqlite-playground/source/teaql-ts/examples/school-management/request-intent.ts
-
curl -L -o request-intent.ts https://huggingface.co/spaces/teaql/browser-sqlite-playground/resolve/main/source/teaql-ts/examples/school-management/request-intent.ts
2.09 kB
| import { Q } from './src/generated/Q'; | |
| import { UserContext } from './src/teaql-ts'; | |
| import { SQLiteTeaQLClient } from './src/teaql-node-sqlite'; | |
| import { RequestIntentError } from 'teaql-ts'; | |
| /** Application-owned probes; never patch the retained generated library. */ | |
| export async function verifyRequestIntent(context: UserContext, client: SQLiteTeaQLClient): Promise<void> { | |
| client.setQueryLoggingEnabled(false).setMutationLoggingEnabled(false); | |
| const before = client.sqlTrace.length; | |
| const reject = async (execute: () => Promise<unknown>, kind: 'query' | 'mutation', field: 'comment' | 'purpose') => { | |
| try { await execute(); } | |
| catch (error) { | |
| if (!(error instanceof RequestIntentError) || error.requestKind !== kind || error.field !== field) throw error; | |
| if (error.code !== (field === 'comment' ? 'REQUEST_COMMENT_REQUIRED' : 'QUERY_PURPOSE_REQUIRED')) throw error; | |
| return; | |
| } | |
| throw new Error(`Missing ${kind} ${field} was accepted`); | |
| }; | |
| try { | |
| await reject(() => Q.schools().limit(1).comment('').purpose('verify request gate').executeForList(context), 'query', 'comment'); | |
| await reject(() => Q.schools().limit(1).comment('\u0085').purpose('verify request gate').executeForList(context), 'query', 'comment'); | |
| await reject(() => Q.schools().limit(1).comment('verify request gate').purpose('').executeForList(context), 'query', 'purpose'); | |
| await reject(() => Q.schools().limit(1).comment('verify request gate').purpose('\u0085').executeForList(context), 'query', 'purpose'); | |
| const school = Q.schools().comment('construct rejected school').purpose('verify request gate').newEntity(context); | |
| await reject(() => school.auditAs('').save(context), 'mutation', 'comment'); | |
| await reject(() => school.auditAs('\u0085').save(context), 'mutation', 'comment'); | |
| if (client.sqlTrace.length !== before) throw new Error('Invalid request reached SQL compilation'); | |
| console.log('PASS TypeScript generated School request-intent gates'); | |
| } finally { | |
| client.setQueryLoggingEnabled(true).setMutationLoggingEnabled(true); | |
| } | |
| } | |