npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oxid

v0.0.5

Published

Isomorphic http request based on rxjs observable

Downloads

9

Readme

Package version Build Status Coverage Node engine version Minified, zipped size

Oxid

oxid is rxjs observable based isomorphic http request module.

Install

This has a peer dependencies of rxjs@6, which will have to be installed as well

npm instll oxid

Usage

Oxid exports default instance oxid, also exposes Oxid class to construct separate instances.

import { oxid, Oxid } from 'oxid';

oxid.get('url', options).subscribe();

const anotherInstance = new Oxid(customConfigurations);
anotherInstance.get('url', options).subscribe();

All of oxid's interface returns Observable<HttpEvent<T>> allows to subscribe into from base request function to http method function.

type requestMethodType = <T>(url: string, config?: RequestConfig) => Observable<HttpEvent<T>>;
type requestMethodWithDataType = <T, U>(url: string, data?: T, config?: RequestConfig) => Observable<HttpEvent<U>>;


class Oxid {
  public readonly delete: requestMethodType;
  public readonly get: requestMethodType;
  public readonly head: requestMethodType;
  public readonly options: requestMethodType;


  public readonly post: requestMethodWithDataType;
  public readonly put: requestMethodWithDataType;
  public readonly patch: requestMethodWithDataType;

  public request<T extends object | string = any>(url: string, config?: RequestConfig): Observable<HttpEvent<T>>;
  public request<T extends object | string = any>(url: string): Observable<HttpEvent<T>>;
  public request<T extends object | string = any>(config: RequestConfig): Observable<HttpEvent<T>>;
  public request<T extends object | string = any>(
    urlOrConfig: RequestConfig | string,
    config?: RequestConfig
  ): Observable<HttpEvent<T>> {

Configure oxid

Oxid includes default set of configuration values. This value will be used when use request instance oxid. When creating new instance via class, it doesn't include any option values by default, have to specify via class constructor. Still, individual method (request() and rest) accepts RequestConfig separately, which will merge into configurations when instance is being created.

interface RequestConfigBase {
  url?: string;
  method?: Method;
  baseURL?: string;
  transformRequest?: Transformer | Array<Transformer>;
  transformResponse?: Transformer | Array<Transformer>;
  headers?: any;
  params?: any;
  paramsSerializer?: (params: any) => string;
  data?: any;
  adapter?: Adapter;
  auth?: BasicCredentials;
  responseType?: ResponseType;
  responseEncoding?: string;
  xsrfCookieName?: string;
  xsrfHeaderName?: string;
  maxContentLength?: number;
  validateStatus?: (status: number) => boolean;
  maxRedirects?: number;
  socketPath?: string | null;


  proxy?: ProxyConfig;
}


interface RequestConfigNode extends RequestConfigBase {
  /**
   * Custom agent to be used in node http request.
   */
  httpAgent?: any;
  /**
   * Custom agent to be used in node https request.
   */
  httpsAgent?: any;
  transport?: { request: typeof import('http').request };
}


interface RequestConfigBrowser extends RequestConfigBase {
  /**
   * Emit progress event for xhr request.
   */
  reportProgress?: boolean;
  withCredentials?: boolean;
}
import {oxid, Oxid, defaultOptions} from 'oxid';

oxid.get(url); //will use `defaultOptions`
oxid.get({url, withCredentials: false}); //will use `defaultOptions`, override `withCredentials`

const another = new Oxid(); //no base configueration
const anotherWithConfig = new oxid({withCredendials: false}) //set base configuration

anotherWithConfig.get({url, withCredentials: false}) //will use config when instance created, override `withCredentials`

Note defaultOptions object is immutable. Changing, reassigning values into existing default configuration value won't work, instead should build new configuration object.

Debugging internals of oxid

Oxid itself doesn't have mechanism to write log. Instead, it exposes a function to wire any logger used in application.

function enableLogger(logger: logFunctionType): void;
function enableLogger(logger: Partial<Logger>): void;

It could be either single function, or object have loglevels like debug, info, warn, error. Notes enableLogger is Global function to affects any instance of oxid, and only starts emitting log once after enableLogger has been called.

import { enableLogger, oxid } from 'oxid';

// logs are not emitted
oxid.get().subscribe();

enableLogger(console.log.bind(console));

// now internal logs will be emitted via console
oxid.get().subscribe();

Building / Testing

Few npm scripts are supported for build / test code.

  • build: Transpiles code to dist.
  • build:clean: Clean up existing build.
  • test: Run unit test. Does not require build before execute test.
  • lint: Run lint over all codebases.

Credits

While this module is NOT officially affiliated, it relies on lot of prior art from axios and @angular/http. You may notice some similar logics and it is expected.