Skip to content

feat(vega-typings): add Typescript Types for vega-loader#4000

Merged
hydrosquall merged 2 commits intomainfrom
cameron.yick/vega-loader-types
Feb 2, 2025
Merged

feat(vega-typings): add Typescript Types for vega-loader#4000
hydrosquall merged 2 commits intomainfrom
cameron.yick/vega-loader-types

Conversation

@hydrosquall
Copy link
Member

@hydrosquall hydrosquall commented Jan 16, 2025

Motivation

  • I was learning about vega-loader, and decided to move the information from the README to the typings file.
  • This removes 3 any types from Loader too!

Changes

  • This should have no runtime impact, it only updates Typescript definitions in vega-typings
  • Note: vega-loader's README does not mention that sanitize takes in context and response as additional options - this is something I noticed from console.log'ing the output of the options. For now I only document that in the Typescript types.
    • I could update the README for vega-loader as well if we want consumers to know about this, or leave it out if this is supposed to be an internal implementation detail.

@hydrosquall hydrosquall self-assigned this Jan 16, 2025
@hydrosquall hydrosquall requested a review from a team as a code owner January 16, 2025 21:44
@hydrosquall
Copy link
Member Author

hydrosquall commented Jan 17, 2025

I'm unclear about how to find out what the errors are in the build (which seem to be formatting related)

image

I've run both yarn lint and yarn format on my source file packages/vega-typings/types/runtime/index.d.ts , neither of which make changes to the current head of this branch.

image

Will open a thread in vega-dev about this.

Update

My error was that formatting commands can't be run from the repository root. I should instead do

cd packages/vega-typings
yarn format

@hydrosquall hydrosquall force-pushed the cameron.yick/vega-loader-types branch from c26db54 to fc44911 Compare January 17, 2025 04:26
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jan 17, 2025

Deploying vega with  Cloudflare Pages  Cloudflare Pages

Latest commit: 81b38ac
Status: ✅  Deploy successful!
Preview URL: https://dae4f8c2.vega-628.pages.dev
Branch Preview URL: https://cameron-yick-vega-loader-typ.vega-628.pages.dev

View logs

/**
* Describes context in which the URI will be used.
*/
context: 'href' | 'image';
Copy link
Member Author

Choose a reason for hiding this comment

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

From code search for how context: ' gets called in the monorepo, I found just 3 sites for href and image.

.sanitize(href, { context: 'href' })

return loader._loader.sanitize(uri, { context: 'href' })

.sanitize(uri, { context: 'image' })

* context:dataflow is used when loader is used for getting data into vega-dataflow
* https://vega.github.io/vega/docs/data/
*/
context: 'dataflow';
Copy link
Member Author

@hydrosquall hydrosquall Jan 17, 2025

Choose a reason for hiding this comment

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

Context seems to be exclusively added by vega-dataflow:

context: 'dataflow',
response: responseType(format && format.type)

http: (uri: string, options: any) => Promise<string>;
load: (uri: string, options?: OptionsWithContext) => Promise<string>;
sanitize: (uri: string, options: OptionsWithContext) => Promise<{ href: string }>;
http: (uri: string, options: Partial<RequestInit>) => Promise<string>;
Copy link
Member Author

Choose a reason for hiding this comment

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

Setting RequestInit based on the signature of what fetch would accept:

const opt = extend({}, this.options.http, options),
type = options && options.response,
response = await fetch(url, opt);

/**
* Informs loader which context the URI will be used in.
*/
type OptionsWithContext =
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps rename this to LoaderOptionsWithContext ?

* Informs loader which context the URI will be used in.
*/
type OptionsWithContext =
| (Partial<BaseLoaderOptions> & {
Copy link
Member

Choose a reason for hiding this comment

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

Why are the options optional now but context is not?

Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I could see, context was provided every time that Vega is internally calling sanitize or load, so it seemed worthwhile to document that this info is available to switch on

External users of vega-loader aren't required to know about this since it context is not mentioned in the previous README except for images. I wouldn't have known about it if I didn't happen to be console what sorts of data was getting passed to sanitize and load when verifying the types.

I suppose we could mark these types as optional since the code seems to run OK if you use vega-loader without also using it inside of vega, but I figured erring on the side of strictness would be good here. Happy to adjust the type if you'd recommend changing it.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine but I wanted us to be aware of this.

/** Allows caller to explicitly set loading mode (local or network request). File mode only applies to server-side rendering. */
mode: 'file' | 'http';
/** Default protocol for protocol-relative URIs, defaults to HTTP */
defaultProtocol: string;
Copy link
Member

Choose a reason for hiding this comment

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

Can we enumerate this?

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like this can be file, http, a string that starts with //, or a bunch of other things: https://en.wikipedia.org/wiki/List_of_URI_schemes

I'll include HTTP and file for a start, but leave a string at the end for those other cases.

@hydrosquall hydrosquall requested a review from domoritz January 30, 2025 05:03
@hydrosquall hydrosquall merged commit 0b6a114 into main Feb 2, 2025
5 checks passed
@hydrosquall hydrosquall deleted the cameron.yick/vega-loader-types branch February 2, 2025 00:20
@lsh lsh mentioned this pull request Feb 24, 2025
lsh added a commit that referenced this pull request Feb 25, 2025
Changes since v5.31.0

**vega-expression**
* Add base64 string encoder/decoder to `vega-expression` and
`vega-interpreter` (via #4009). (Thanks @hydrosquall!)

**vega-typings**
* Add Typescript Types for `vega-loader` (via #4000). (Thanks
@hydrosquall!)

**docs**
* Correct data year citation in dorling-cartogram example (via #4006).
(Thanks @dsmedia!)
* Update typo in vega.timeFloor description (via #4010). (Thanks
@hydrosquall!)
* Add Security Advisory Policy for Vega (via #4008). (Thanks
@hydrosquall!)
* Replace redirect url in `expressions.md` (via #3996). (Thanks
@dangotbanned!)
* correct queries to query in `crossfilter.md` (via #4005). (Thanks
@danmarshall!)

---------

Signed-off-by: Lukas Hermann <1734032+lsh@users.noreply.github.com>
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.

3 participants