Giter Club home page Giter Club logo

magic-odata's Introduction

magic-odata

Magical OData client generation for typescript. No more string. No more any!

Node.js CI NPM Coverage Status License

Install

npm i magic-odata-code-gen --save-dev; npm i magic-odata-client;

  • magic-odata-code-gen is a dev dependency required to generate a client at compile time
    • This tool is small and we recommend installing it locally per project in order to avoid versioning issues
  • magic-odata-client is a dependency of your application. The generated code will use this package

Generate

node node_modules/magic-odata-code-gen/dist/index.js --metadataUrl "https://raw.githubusercontent.com/ShaneGH/magic-odata/main/docs/sampleOdataMetadata.xml"

Use

// import a generated client
import { ODataClient } from "./odataClient.js"

// create a client instance
const oDataClient = new ODataClient({
    // inject a basic Http client
    request: (input, init) => fetch(input, init),

    // add a root URI
    uriRoot: "https://my.odata.server/odata"
})

// use the client!
const popularBlogPosts = oDataClient.BlogPosts
    // Use object deconstruction to choose query tools
    .withQuery((blogPost, {
        $orderby: {orderBy}, 
        $skip, 
        $top
    }) => [ 
        // Combine query tools and fluent operations to build a query
        blogPost.Comments.$count.gt(100).or(blogPost.Likes.gt(100)),
        orderBy(blogPost.Name),
        $skip(0),
        $top(10)
    ])
    .get();

Why?

Write safe, statically typed odata queries in typescript. No more string. No more any!

  • Cut down on runtime errors
  • Explore OData API possibilities in your IDE
  • Runs in the browser or in node
  • No prod dependencies. Small bundle size
    • Generated executable code is tiny. Almost all generated code is type information
    • magic-odata-client minifies and compresses to ~10KB
  • Optional angular mode, for the angular HttpClient

Features

See Features.md

  • Code gen configuration
    • $metadata files protected with authentication
    • Angular mode
  • Client configuration
  • OData GET requests
  • OData URI generation
  • Entity keys
  • Entity path
  • @Parameter aliases
  • Enums
  • Functions
  • Indexed ordered collections
  • Query options
    • $filter
      • fluent comparison operators
    • $select
    • $expand
      • $levels
    • $orderBy
    • $search
    • $skip, $top and $count
    • custom
    • $root
  • Casting
  • $value

Contributing

See Contributing.md

magic-odata's People

Contributors

shanegh avatar

Watchers

 avatar  avatar

magic-odata's Issues

$value request

new ODataClient(...).Users.withKey(x).subPath(x => x.Name).subPath(x => x.$value)

Alternative

Hi,
I'm really unsure if I should open this ticket, but here I go.
I'm the author of odata2ts. And it actually does what magic-odata does. Have you considered this as alternative?

Don't get me wrong: It's always cool to have alternatives and there also exist quite different reasons to have them. But since I know how long the road is to generate a full featured OData client (nearly impossible ๐Ÿ˜‰)...

Anyways, I hope I don't step on your toes by writing this.

But then again, I'm curious and if you have considered it as alternative and wanted to have a different tool, then I would be highly interested in those reasons.

User can do illegal queries on singletons

EntitySet.withQuery when the current entity is a singleton has all of the same functionality as an entity Collection

From ASPNET:
The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections

Add test for cast entity name, if name contains namesapace

Casting tests all look like this:

await client.HasIds
    .withKey(x => x.key(user.Id!))
    .cast(i => i.Blog())
    .get()

Need a test like this

await client.HasIds
    .withKey(x => x.key(user.Id!))
    .cast(i => i.My_Odata_Entities_Blog())
    .get()

Expose query builder, detached from Entityset

Like

const queryParts = userQuery((user, {filter: {eq}}) => eq(user.id, 123))
console.log(queryParts.$filter)

Also, remove exposed buildComplexTypeRef from magic-odata-client index.ts

Add missing functions (p0)

Compatibility with verbatimModuleSyntax

The compile option verbatimModuleSyntax was added in TypeScript 5.0.
If this option is true, imports of types that are not used in the code or only used as type names will fail without the type modifier.

At the top of the generated OData Client code, many types are imported, including those that are not used.
To avoid compile errors, could you please make sure that unused types are not imported, or add the type modifier appropriately?

Fix integer based enums in query data params

Search for this text: NOTE: enum values where the enum is represented by a number in typescript are currently not supported. Use createRawConst instead

Search for this issue

Notes:
The issue is that the "Const" ParameterDefinition returned by createConst does not have any type info. The only way I can see at the moment is to mutate it after generation in multiple places in code.

  • In key lookup, where the key type is known
  • In function calls where the function type is known
  • In query parts, using the type that it is being compared against

None of this is ideal and would be susceptible to bugs

Whitelisted types should automatically whitelist their properties

Whitelisted types should automatically whitelist their Complex object and Enum properties (but not navigation properties)

e.g. in test framework

  • Whitelist User should auto whitelist UserType
  • Whitelist Comment should auto whitelist Tag
  • Whitelist Blog should not whitelist User or BlogPost

Add support or Geo types

GeographyPoint = "GeographyPoint",
GeographyLineString = "GeographyLineString",
GeographyPolygon = "GeographyPolygon",
GeographyMultiPoint = "GeographyMultiPoint",
GeographyMultiLineString = "GeographyMultiLineString",
GeographyMultiPolygon = "GeographyMultiPolygon",
GeographyCollection = "GeographyCollection",
GeometryPoint = "GeometryPoint",
GeometryLineString = "GeometryLineString",
GeometryPolygon = "GeometryPolygon",
GeometryMultiPoint = "GeometryMultiPoint",
GeometryMultiLineString = "GeometryMultiLineString",
GeometryMultiPolygon = "GeometryMultiPolygon",
GeometryCollection = "GeometryCollection",

subPathRaw function

const comment = await client.BlogPosts
    .withKey(x => x.keyRaw(`'${user.blogPost.Id}'`))
    .subPathRaw("Comments") // something like this. The interface needs to be decided
    .withKey(x => x.key(user.comment.Id!, WithKeyType.PathSegment))
    .get();

Functions

Fix some issues in precedence

1. Allow proper querying after a $count

client.Users.subPath(x => x.$count).withQuery(...)

2. Allow proper querying after a $value

client.Users.withKey(123).subPath(x => x.namr).subPath(x => x.$value).withQuery(...)

3. Disallow path/cast after query

client.Users.withKey(123).withQuery(...).subPath(x => x.Blogs)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.