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

chore(gatsby): Migrate utils/get-latest-apis.js to ts #22097

Merged
merged 11 commits into from
May 21, 2020

Conversation

cola119
Copy link
Contributor

@cola119 cola119 commented Mar 9, 2020

Description

Convert utils/get-latest-apis to typescript

Documentation

Related Issues

Related to #21995

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
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 want to use require in order mock objects to be inferred as any. I came up with using as jest.Mock<> as an alternative but I guess this is a bit redundant and no different from using any.

try {
const { data } = await axios.get(API_FILE, { timeout: 5000 })

await fs.writeFile(OUTPUT_FILE, JSON.stringify(data, null, 2), `utf8`)

return data
} catch (e) {
if (await fs.exists(OUTPUT_FILE)) {
if (await fs.existsSync(OUTPUT_FILE)) {
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 think we should use a sync method.

Copy link
Contributor

Choose a reason for hiding this comment

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

How come? I don't see the benefit, but happy to be convinced!

Copy link
Contributor

Choose a reason for hiding this comment

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

We await for the async one, so we could use the sync one instead.
But need to delete the await key then

Copy link
Contributor

Choose a reason for hiding this comment

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

Still want to understand why we want the sync one. This does put a different expense on the system. When you await things, you allow the node process to handle other things on the stack that are paused. I'm not seeing the benefit of doing sync here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, when I search for fs-extra I found out that fs.exist(OUTPUT_FILE) returns a promise. So I shouldn't change to existSync. 🙇

fs-extra is a drop in replacement for native fs. All methods in fs are attached to fs-extra. All fs methods return promises if the callback isn't passed.
ref

However, this is a bit troublesome because TS infers fs.exists as native fs.exists. The error An argument for 'callback' was not provided. still remains.

function exists(path: PathLike, callback: (exists: boolean) => void): void;

And be careful that exist() of native fs returns void, not a promise and it needs a callback function on second argument. And this is deprecated.

Copy link
Contributor

Choose a reason for hiding this comment

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

looks like we just want this instead: https://github.com/jprichardson/node-fs-extra/blob/master/docs/pathExists.md

if this doesn't work, I think the best thing would be to submit a PR to update the types in DefinitelyTyped

Comment on lines 9 to 13
export interface IAPIResponse {
[key: string]: any
}

export const getLatestAPIs = async (): Promise<IAPIResponse> => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to define the interface of API response but this was too difficult for me. plz help 🙏

Copy link
Contributor

Choose a reason for hiding this comment

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

it looks like it's some object with { browser: {}, node: {}, ssr: {} }, I would probably just add a console.log to data here and run the code so you can see what it prints out.

I think this gets triggered by having a gatsby plugin with exports that are not part of the gatsby spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It has so many properties but I determined below for now.

interface IAPIResponse {
  ssr: {
    [key: string]: object
  }
  browser: {
    [key: string]: object
  }
  node: {
    [key: string]: object
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

i'd prefer to be explicit. Let's properly type it please!

blainekasten
blainekasten previously approved these changes May 12, 2020
Copy link
Contributor

@blainekasten blainekasten left a comment

Choose a reason for hiding this comment

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

This looks great to me, let's merge it!

Thank you so much for contributing to our TypeScript refactor! We have more work to do and we would love to have you stay involved in our transition. Please submit more PRs! 💜

@blainekasten blainekasten added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label May 12, 2020
packages/gatsby/src/utils/get-latest-apis.ts Outdated Show resolved Hide resolved
blainekasten
blainekasten previously approved these changes May 20, 2020
@blainekasten blainekasten merged commit b81b604 into master May 21, 2020
@delete-merged-branch delete-merged-branch bot deleted the chore/ts/get_lates_apis branch May 21, 2020 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants