Skip to content

Commit 42a99aa

Browse files
committed
chore(): support for formatting and api schema
1 parent 83bd907 commit 42a99aa

40 files changed

+1313
-612
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@awell-health/awell-score",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Library of Medical Score functions",
55
"packageManager": "[email protected]",
66
"main": "dist/index.js",

src/classes/Score.ts

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import {
44
type ScoreOutputSchemaType,
55
type ScoreType,
66
type CalculateFn,
7+
type CategoryType,
8+
type TerminologyType,
79
} from '../types'
810
import {
911
parseReadmeToHtml,
1012
getUnionType,
1113
createZodObjectFromSchema,
14+
parseToAwellApiSchema,
1215
} from '../lib'
1316
import {
1417
tryCastToBoolean,
@@ -19,6 +22,7 @@ import {
1922
} from '../lib/castFunctions'
2023
import _ from 'lodash'
2124
import { simulateDateInput, simulateStringInput } from '../lib/simulation'
25+
import { parseToApiResultFormat } from '../lib/parseToApiResultFormat'
2226

2327
/**
2428
* Class representing a Score, which calculates results based on input and output schemas.
@@ -30,6 +34,11 @@ export class Score<
3034
OutputSchema extends ScoreOutputSchemaType,
3135
> implements Omit<ScoreType<InputSchema, OutputSchema>, 'calculate'>
3236
{
37+
/**
38+
* The ID of the score.
39+
*/
40+
id: string
41+
3342
/**
3443
* The name of the score.
3544
*/
@@ -55,6 +64,11 @@ export class Score<
5564
*/
5665
outputSchema: OutputSchema
5766

67+
/**
68+
* The FHIR terminology of the score.
69+
*/
70+
terminology: TerminologyType | undefined
71+
5872
/**
5973
* The function to calculate the score.
6074
*/
@@ -76,15 +90,18 @@ export class Score<
7690
*/
7791
public constructor(
7892
score: ScoreType<InputSchema, OutputSchema> & {
93+
id?: string
7994
inputSchema: InputSchema
8095
outputSchema: OutputSchema
8196
},
8297
) {
98+
this.id = score.id ?? score.name
8399
this.name = score.name
84100
this.readme_location = score.readme_location
85101
this.description = parseReadmeToHtml(this.readme_location)
86102
this.inputSchema = score.inputSchema
87103
this.outputSchema = score.outputSchema
104+
this.terminology = score.terminology
88105
this._calculate = score.calculate
89106
this.inputSchemaAsObject = createZodObjectFromSchema(this.inputSchema)
90107
}
@@ -103,35 +120,28 @@ export class Score<
103120
* @default false
104121
*/
105122
strictMode?: boolean
106-
/**
107-
* Specifies the format of the output:
108-
* - `simple`: Key-value where the key is the result ID and the value is the result.
109-
* - `awell`: Enriched array format.
110-
* @default 'simple'
111-
*/
112-
outputFormat?: 'simple' | 'awell'
113123
}
114124
}): Record<
115125
keyof OutputSchema,
116126
z.infer<OutputSchema[keyof OutputSchema]['type']> | null
117127
> {
118-
const parsedData = this.inputSchemaAsObject.parse(
128+
const d = this.inputSchemaAsObject.parse(
119129
params?.opts?.strictMode === true
120130
? params.payload
121-
: this.castInputsToExactTypes(params.payload),
131+
: this.tryCastInputsToExactTypes(params.payload),
122132
)
123133

124134
return this._calculate({
125-
data: parsedData,
135+
data: d,
126136
})
127137
}
128138

129139
/**
130-
* Casts input values to their exact types based on the input schema.
140+
* Tries to cast input values to their exact types based on the input schema.
131141
* @param input - The input data to cast.
132142
* @returns The cast input data.
133143
*/
134-
castInputsToExactTypes(
144+
tryCastInputsToExactTypes(
135145
data: Record<string, unknown>,
136146
): Record<string, unknown> {
137147
/**
@@ -204,7 +214,10 @@ export class Score<
204214
*/
205215
simulate(): {
206216
simulatedInput: Record<string, unknown>
207-
simulatedOutput: Record<string, unknown>
217+
results: Record<
218+
keyof OutputSchema,
219+
z.infer<OutputSchema[keyof OutputSchema]['type']> | null
220+
>
208221
} {
209222
const simulatedInput = _.mapValues(
210223
this.inputSchemaAsObject.shape,
@@ -266,7 +279,52 @@ export class Score<
266279

267280
return {
268281
simulatedInput: _.pickBy(simulatedInput, value => value !== undefined),
269-
simulatedOutput: this.calculate({ payload: simulatedInput }),
282+
results: this.calculate({ payload: simulatedInput }),
270283
}
271284
}
285+
286+
/**
287+
* Formats the results of the score calculation.
288+
* @param results - The results to format.
289+
* @param opts - The options for formatting the results.
290+
* @returns The formatted results.
291+
*/
292+
formatResults(
293+
results: Record<
294+
keyof OutputSchema,
295+
z.infer<OutputSchema[keyof OutputSchema]['type']> | null
296+
>,
297+
{
298+
opts,
299+
}: {
300+
/**
301+
* Specifies the format of the output:
302+
* - `simple`: Key-value where the key is the result ID and the value is the result.
303+
* - `awell`: Enriched array format.
304+
* @default 'simple'
305+
*/
306+
opts?: { format: 'simple' | 'awell' }
307+
},
308+
) {
309+
if (opts?.format === 'simple') {
310+
return results
311+
}
312+
313+
return parseToApiResultFormat<OutputSchema>(results, this.outputSchema)
314+
}
315+
316+
/**
317+
* Returns the API schema for the score.
318+
* @returns The API schema for the score.
319+
*/
320+
get apiSchema() {
321+
return parseToAwellApiSchema({
322+
scoreId: this.id,
323+
scoreName: this.name,
324+
scoreDescription: this.description,
325+
inputSchema: this.inputSchema,
326+
outputSchema: this.outputSchema,
327+
terminology: this.terminology,
328+
})
329+
}
272330
}

src/classes/__tests__/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test README file.

0 commit comments

Comments
 (0)