Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add typescript into the project #356

wants to merge 1 commit into from

Conversation

voxelias
Copy link
Contributor

This PR:

  • Adds typescript to the project
  • Turns index.js into index.ts and add types necessary to run the module
  • Adds possibility to import css into typescript modules
  • Makes no functional changes

What to test:

  1. Build project
  2. Deploy target static files onto a printer
  3. Make sure UI works

@voxelias voxelias requested a review from ondratu February 14, 2023 12:26
@voxelias voxelias self-assigned this Feb 14, 2023
package.json Outdated
},
"dependencies": {
"source-map-loader": "^4.0.1",
"ts-loader": "^9.4.2"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those can remain devDependencies ;)

Copy link
Contributor Author

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",
Copy link
Collaborator

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

Suggested change
"target": "es5",
"target": "ES2017",
"module": "esnext",
"moduleResolution": "node",

Copy link
Contributor Author

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"
}
Copy link
Collaborator

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?

Copy link
Contributor Author

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>)
Copy link
Collaborator

@ddx32 ddx32 Feb 15, 2023

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.

Copy link
Collaborator

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;
}

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants