Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
oetzit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
commul
oetzit
Commits
4841e052
Commit
4841e052
authored
3 years ago
by
Paolo.Brasolin
Browse files
Options
Downloads
Patches
Plain Diff
feat: #be sketch cleanly typed API
parent
7cddffe5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend/src/api.ts
+103
-0
103 additions, 0 deletions
backend/src/api.ts
backend/src/index.ts
+3
-0
3 additions, 0 deletions
backend/src/index.ts
with
106 additions
and
0 deletions
backend/src/api.ts
0 → 100644
+
103
−
0
View file @
4841e052
import
{
FastifyPluginCallback
}
from
"
fastify
"
;
import
{
FromSchema
}
from
"
json-schema-to-ts
"
;
import
{
connection
}
from
"
./db
"
;
// NOTE: see https://www.npmjs.com/package/fastify-plugin for TS plugin definition
const
ParamsSchema
=
{
type
:
"
object
"
,
properties
:
{
id
:
{
type
:
"
string
"
,
format
:
"
uuid
"
,
},
},
required
:
[
"
id
"
],
}
as const
;
const
GameSchema
=
{
type
:
"
object
"
,
properties
:
{
id
:
{
type
:
"
string
"
,
format
:
"
uuid
"
,
},
},
required
:
[
"
id
"
],
}
as const
;
const
WordSchema
=
{
type
:
"
object
"
,
properties
:
{
id
:
{
type
:
"
string
"
,
format
:
"
uuid
"
},
image
:
{
type
:
"
string
"
},
ocr_confidence
:
{
type
:
"
number
"
,
minimum
:
0
,
maximum
:
1
},
ocr_transcript
:
{
type
:
"
string
"
},
},
required
:
[
"
id
"
],
}
as const
;
const
apiPlugin
:
FastifyPluginCallback
=
(
fastify
,
options
,
next
)
=>
{
fastify
.
route
<
{
Reply
:
FromSchema
<
typeof
WordSchema
>
;
}
>
({
method
:
"
GET
"
,
url
:
"
/word
"
,
schema
:
{
response
:
{
200
:
WordSchema
,
404
:
{},
// TODO: JSend error
},
},
handler
:
async
(
request
,
reply
)
=>
{
// TODO: scope this to game to avoid collisions
const
word
=
await
connection
<
FromSchema
<
typeof
WordSchema
>>
(
"
words
"
)
.
whereBetween
(
"
ocr_confidence
"
,
[
0.4
,
0.8
])
// i.e. needs improvement but it's not trash
.
whereRaw
(
`"ocr_transcript" ~ '^[[:alpha:]]+$'`
)
// i.e. no numbers nor symbols
.
orderByRaw
(
"
RANDOM()
"
)
.
first
();
if
(
word
===
undefined
)
{
reply
.
code
(
404
);
}
else
{
reply
.
send
({
id
:
word
.
id
,
image
:
word
.
image
,
ocr_confidence
:
word
.
ocr_confidence
,
ocr_transcript
:
word
.
ocr_transcript
,
});
}
},
});
fastify
.
route
<
{
Params
:
FromSchema
<
typeof
ParamsSchema
>
;
Reply
:
FromSchema
<
typeof
GameSchema
>
;
}
>
({
method
:
"
GET
"
,
url
:
"
/games/:id
"
,
schema
:
{
params
:
ParamsSchema
,
response
:
{
200
:
GameSchema
,
404
:
{},
// TODO: JSend error
},
},
handler
:
async
(
request
,
reply
)
=>
{
const
game
=
await
connection
<
FromSchema
<
typeof
GameSchema
>>
(
"
games
"
)
.
where
(
"
id
"
,
request
.
params
.
id
)
.
first
();
if
(
game
===
undefined
)
{
reply
.
code
(
404
);
}
else
{
reply
.
send
({
id
:
game
.
id
,
});
}
},
});
next
();
};
export
default
apiPlugin
;
This diff is collapsed.
Click to expand it.
backend/src/index.ts
+
3
−
0
View file @
4841e052
...
@@ -48,6 +48,9 @@ server.get("/", function (request, reply) {
...
@@ -48,6 +48,9 @@ server.get("/", function (request, reply) {
reply
.
code
(
200
).
send
(
"
Hello, World!
"
);
reply
.
code
(
200
).
send
(
"
Hello, World!
"
);
});
});
import
apiRoutes
from
"
./api
"
;
server
.
register
(
apiRoutes
,
{
prefix
:
"
api
"
});
server
.
route
({
server
.
route
({
method
:
"
POST
"
,
method
:
"
POST
"
,
url
:
"
/GetImage
"
,
url
:
"
/GetImage
"
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment