-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add typescript into the project #356
base: master
Are you sure you want to change the base?
Conversation
package.json
Outdated
}, | ||
"dependencies": { | ||
"source-map-loader": "^4.0.1", | ||
"ts-loader": "^9.4.2" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those can remain devDependencies
;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
tsconfig.json
Outdated
"compilerOptions": { | ||
"outDir": "./build", | ||
"allowJs": true, | ||
"target": "es5", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just going through some errors my IDE is throwing. This would be required for TypeScript to be happy with dynamic imports, file resolutions and recent features like Object.values
"target": "es5", | |
"target": "ES2017", | |
"module": "esnext", | |
"moduleResolution": "node", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had no complains, but I've changed the config
}, | ||
"dependencies": { | ||
"source-map-loader": "^4.0.1", | ||
"ts-loader": "^9.4.2" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add @types/node
so we can get typehints on things like process.env.UPDATE_INTERVAL
etc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
src/index.ts
Outdated
@@ -86,7 +111,7 @@ async function getRequests(initialized) { | |||
const promises = Object.values(apiRequests); | |||
const responses = await Promise.all( | |||
promises.map((i) => | |||
i | |||
(i as Promise<any>) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be ideal to avoid type casting here, but that would require broader rethinking and refactoring of the way apiRequests
variable is constructed.
Also, the shouldGet
method now seems to be mixing up number
and Date
data types when performing operations on the timestamp
key of the ApiRequest
object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be one of the ways of slight refactoring that would retain type safety (and help correct the errors in the Date/number types that Typescript unveiled).
I did not test this! Use as a pointer only 😉
Merge the ApiRequest
/Requests
data types:
type RequestType = 'printer' | 'profiles' | 'job' | 'connection';
interface ApiRequest {
type: RequestType,
get: () => Promise<any>,
init: boolean,
update: boolean,
updateInterval?: number,
timestamp?: Date,
}
Change the requests
object into an array:
const requests: ApiRequest[] = [
{
type: 'printer',
get: () => getJson("/api/printer"),
init: true,
update: true,
},
{
// NOTE: I leave it like this until we change the API
type: 'profiles',
get: () =>
process.env.PRINTER_TYPE === "sla"
? getJson("/api/printerprofiles")
: new Promise((resolve) => resolve({})),
init: true,
update: false,
},
// ...etc
]
Simplify the getRequests
function to work with the above array directly. This way the IDE/compiler should pick up the types correctly and will highlight the type errors in the timestamp handling bits:
async function getRequests(initialized) {
const timestamp = new Date().getTime();
const apiRequests = requests.map((request) => {
const shouldGet = () => {
if (!initialized) return request.init;
if (request.update) {
if (!request.updateInterval) return true;
if (!request.timestamp)
request.timestamp = timestamp + request.updateInterval;
if (timestamp >= request.timestamp) {
request.timestamp = timestamp + request.updateInterval;
return true;
}
}
};
return shouldGet() ? request.get() : undefined;
})
const responses = await Promise.all(
apiRequests.map((request) =>
request
.then((payload) => ({ ok: true, payload }))
.catch((error) => ({ ok: error.code ? false : null, error }))
)
);
const result = Object.fromEntries(
Object.entries(apiRequests).map(([key], i) => [key, responses[i]])
);
return result;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've rewritten the whole code. At the point I was writing it, I wanted to have it as simple as possible. With V1 API it makes more sense of course. So I've added there also a module for API with types.
This PR:
What to test: