Skip to content

Commit

Permalink
Update types
Browse files Browse the repository at this point in the history
  • Loading branch information
huulbaek committed Nov 21, 2023
1 parent 80c92d5 commit d5a8016
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
Binary file modified bun.lockb
Binary file not shown.
8 changes: 4 additions & 4 deletions src/calculations/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { ISpace } from './interfaces/space'
import { IConfig } from './interfaces/config'
import { ISpaceCalculation } from './interfaces/space_calculation'
import { ISpaceResult } from './interfaces/space_result'
import { ISpaceConstant } from './interfaces/space_constant'
import { IVariable } from './interfaces/variable'
import { IConstant } from './interfaces/constant'
import { ICalculationResult } from './interfaces/calculationresult'
import { TCustomSpaceConstants } from './types/custom_space_constant'
import getSpace from './spaces'
import Dummy from './spaces/dummy'

Expand All @@ -16,8 +16,8 @@ import Dummy from './spaces/dummy'
*/
export default class Calculator {
variables: IVariable
customSpaceConstants: ISpaceConstant
customConstants: IConstant
customSpaceConstants?: TCustomSpaceConstants
customConstants?: IConstant
config: IConfig
constants: IConstant
totalWorkplaceArea: number = 0
Expand All @@ -34,7 +34,7 @@ export default class Calculator {
* @param {IConstant} customConstants – Custom constants
* @param {string} [configFile] – The config file to use
*/
constructor(variables: IVariable, customSpaceConstants: ISpaceConstant, customConstants: IConstant , configFile: string = 'default.json') {
constructor(variables: IVariable, customSpaceConstants: TCustomSpaceConstants|undefined, customConstants: IConstant|undefined , configFile: string = 'default.json') {
this.variables = variables
this.customSpaceConstants = customSpaceConstants
this.customConstants = customConstants
Expand Down
9 changes: 9 additions & 0 deletions src/calculations/interfaces/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IVariable } from './variable'
import { IConstant } from './constant'
import { TCustomSpaceConstants } from '../types/custom_space_constant'

export interface IRequest {
variables: IVariable
customConstants?: IConstant
customSpaceConstants?: TCustomSpaceConstants
}
3 changes: 1 addition & 2 deletions src/calculations/spaces/main_space_class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { ISpaceCalculation } from '../interfaces/space_calculation'
import { IConstant } from '../interfaces/constant'
import { IVariable } from '../interfaces/variable'
import { findSpace } from '../helpers'

type TCustomSpaceConstants = {[key:string]:ISpaceConstant}
import { TCustomSpaceConstants } from '../types/custom_space_constant'

/**
* This is the main class for the spaces. It contains shared methods for all spaces.
Expand Down
3 changes: 3 additions & 0 deletions src/calculations/types/custom_space_constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ISpaceConstant } from '../interfaces/space_constant'

export type TCustomSpaceConstants = {[key:string]:ISpaceConstant}
19 changes: 7 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ISpaceConstant } from './calculations/interfaces/space_constant'
import { IConstant } from './calculations/interfaces/constant'
import { IVariable } from './calculations/interfaces/variable'
import { IRequest } from './calculations/interfaces/request'
import Calculator from './calculations/calculator'
import redis from 'redis'
import { RedisClientType } from '@redis/client'
import { TCustomSpaceConstants } from './calculations/types/custom_space_constant'

let redisClient: RedisClientType
if (process.env.USE_CACHE_REDIS === '1') {
Expand Down Expand Up @@ -32,22 +33,16 @@ Bun.serve({
const url = new URL(req.url)
if (url.pathname === "/") return new Response("Agiliate is running", { headers })
if (url.pathname === "/calculate") {
const jsonReq = await req.json()
const jsonReq: IRequest = await req.json() as IRequest
if (process.env.CACHE === '1') {
const cachedResult = await redisClient.get(jsonReq)
const cachedResult = await redisClient.get(JSON.stringify(jsonReq))
if (cachedResult) {
return Response.json(JSON.parse(cachedResult), { headers })
}
}
const variables: IVariable = {
...jsonReq.variables
}
const customSpaceConstants: ISpaceConstant = {
...jsonReq?.customSpaceConstants
}
const customConstants: IConstant = {
...jsonReq?.customConstants
}
const variables: IVariable = jsonReq.variables
const customSpaceConstants: TCustomSpaceConstants|undefined = jsonReq?.customSpaceConstants
const customConstants: IConstant|undefined = jsonReq?.customConstants
const calculator = new Calculator(variables, customSpaceConstants, customConstants)
const result = calculator.result()
if (process.env.CACHE === '1')
Expand Down

0 comments on commit d5a8016

Please sign in to comment.