Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

magic-odata-shared

ShaneGH26MIT1.1.1TypeScript support: included

Shared interfaces between magic-odata-client and magic-odata-code-gen

odata, query, client, odata-client, typescript, generated, auto-generated, code-generation, angular

readme

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

changelog

v1.1.0

  • Added fluent operators

v1.0.3

  • Added serialize -INF
  • Fixed issues with readme in npm
  • increased code coverage

v1.0.2

  • Added Linting
  • Added CI build
  • Added Code coverage
  • Added Changelog

v1.0.1

  • Some code comment and documentation updates

v1.0.0

Initial release. Minimum set of functionality including

  • Generated entity types
  • Query by:
    • path
    • key
    • index
    • query params
  • Angular mode