Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@internetarchive/ads-library

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@internetarchive/ads-library - npm Package Compare versions

Comparing version 0.0.8 to 0.1.0

18

.rollup.cache/Users/jeffwklein/work/archive/ads-common/packages/ads-library/dist/api.d.ts

@@ -1,8 +0,18 @@

export declare function callJsonApi<Rq, Rsp>(options: {
export type ApiRequestMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
export interface BaseApiFetchOptions<RequestT> {
url: string;
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
method: ApiRequestMethod;
errorMessage: string;
body?: RequestT;
}
export type ExtendedApiFetchOptions<RequestT, ExtraOptionsT> = BaseApiFetchOptions<RequestT> & ExtraOptionsT;
export type ApiFetchOptions<RequestT> = ExtendedApiFetchOptions<RequestT, {
acceptType: string;
contentType?: string;
}>;
export type JsonApiFetchOptions<RequestT> = ExtendedApiFetchOptions<RequestT, {
disableCamelize?: boolean;
body?: Rq;
}): Promise<Rsp>;
}>;
export declare function callJsonApi<RequestT, ResponseT>(options: JsonApiFetchOptions<RequestT>): Promise<ResponseT>;
export declare function callApi<RequestT extends BodyInit | null | undefined>(options: ApiFetchOptions<RequestT>): Promise<Response>;
/**

@@ -9,0 +19,0 @@ * Gets an object describing a CSRF header taking the value of the CSRF token

import camelcaseKeys from "camelcase-keys";
// calls our base callApi method and returns a json type, has option to disable response camel-casing
export function callJsonApi(options) {
return (callApi({
...options,
body: options.body ? JSON.stringify(options.body) : undefined,
acceptType: "application/json",
contentType: "application/json",
})
.then((response) => response.json())
// camelize response keys unless disabled, cast to generic response type
.then((json) => options.disableCamelize
? json
: camelcaseKeys(json, { deep: true })));
}
// generic interface on top of JS fetch function. adds:
// - csrf token
// - friendly interface for accept/content type
// - error handling and logging consistent with ADS patterns
export function callApi(options) {
const request = {

@@ -7,8 +25,13 @@ method: options.method,

...getCsrfHeader(),
Accept: "application/json",
"Content-Type": "application/json",
Accept: options.acceptType,
},
body: options.body,
};
if (options.body) {
request.body = JSON.stringify(options.body);
// to add optional Content-Type, we must reinitialize headers after type refinement
// since HeadersInit doesn't accept optional/mutable types.
if (options.contentType && request.headers) {
request.headers = {
...request.headers,
"Content-Type": options.contentType,
};
}

@@ -18,3 +41,3 @@ return (fetch(options.url, request)

if (response.ok) {
return response.json();
return response;
}

@@ -25,6 +48,2 @@ else {

})
// camelize response keys unless disabled, cast to generic response type
.then((json) => options.disableCamelize
? json
: camelcaseKeys(json, { deep: true }))
// log error, return no response

@@ -31,0 +50,0 @@ .catch((error) => {

export { EventHelpers } from "./events";
export { callJsonApi, getCsrfHeader } from "./api";
export { callApi, callJsonApi, getCsrfHeader, ApiFetchOptions, JsonApiFetchOptions, ApiRequestMethod, BaseApiFetchOptions, ExtendedApiFetchOptions, } from "./api";
export { UserOperatingSystem, getUserOS } from "./browser";
export { humanBytes, formatDate } from "./formats";
// publicly available library functions
export { EventHelpers } from "./events";
export { callJsonApi, getCsrfHeader } from "./api";
export { callApi, callJsonApi, getCsrfHeader, } from "./api";
export { UserOperatingSystem, getUserOS } from "./browser";
export { humanBytes, formatDate } from "./formats";
//# sourceMappingURL=index.js.map

@@ -1,8 +0,18 @@

export declare function callJsonApi<Rq, Rsp>(options: {
export type ApiRequestMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
export interface BaseApiFetchOptions<RequestT> {
url: string;
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
method: ApiRequestMethod;
errorMessage: string;
body?: RequestT;
}
export type ExtendedApiFetchOptions<RequestT, ExtraOptionsT> = BaseApiFetchOptions<RequestT> & ExtraOptionsT;
export type ApiFetchOptions<RequestT> = ExtendedApiFetchOptions<RequestT, {
acceptType: string;
contentType?: string;
}>;
export type JsonApiFetchOptions<RequestT> = ExtendedApiFetchOptions<RequestT, {
disableCamelize?: boolean;
body?: Rq;
}): Promise<Rsp>;
}>;
export declare function callJsonApi<RequestT, ResponseT>(options: JsonApiFetchOptions<RequestT>): Promise<ResponseT>;
export declare function callApi<RequestT extends BodyInit | null | undefined>(options: ApiFetchOptions<RequestT>): Promise<Response>;
/**

@@ -9,0 +19,0 @@ * Gets an object describing a CSRF header taking the value of the CSRF token

export { EventHelpers } from "./events";
export { callJsonApi, getCsrfHeader } from "./api";
export { callApi, callJsonApi, getCsrfHeader, ApiFetchOptions, JsonApiFetchOptions, ApiRequestMethod, BaseApiFetchOptions, ExtendedApiFetchOptions, } from "./api";
export { UserOperatingSystem, getUserOS } from "./browser";
export { humanBytes, formatDate } from "./formats";

@@ -552,3 +552,21 @@ // static class that encapsulates shorthands and utilities for emitting events.

// calls our base callApi method and returns a json type, has option to disable response camel-casing
function callJsonApi(options) {
return (callApi({
...options,
body: options.body ? JSON.stringify(options.body) : undefined,
acceptType: "application/json",
contentType: "application/json",
})
.then((response) => response.json())
// camelize response keys unless disabled, cast to generic response type
.then((json) => options.disableCamelize
? json
: camelcaseKeys(json, { deep: true })));
}
// generic interface on top of JS fetch function. adds:
// - csrf token
// - friendly interface for accept/content type
// - error handling and logging consistent with ADS patterns
function callApi(options) {
const request = {

@@ -558,8 +576,13 @@ method: options.method,

...getCsrfHeader(),
Accept: "application/json",
"Content-Type": "application/json",
Accept: options.acceptType,
},
body: options.body,
};
if (options.body) {
request.body = JSON.stringify(options.body);
// to add optional Content-Type, we must reinitialize headers after type refinement
// since HeadersInit doesn't accept optional/mutable types.
if (options.contentType && request.headers) {
request.headers = {
...request.headers,
"Content-Type": options.contentType,
};
}

@@ -569,3 +592,3 @@ return (fetch(options.url, request)

if (response.ok) {
return response.json();
return response;
}

@@ -576,6 +599,2 @@ else {

})
// camelize response keys unless disabled, cast to generic response type
.then((json) => options.disableCamelize
? json
: camelcaseKeys(json, { deep: true }))
// log error, return no response

@@ -668,3 +687,3 @@ .catch((error) => {

export { EventHelpers, UserOperatingSystem, callJsonApi, formatDate, getCsrfHeader, getUserOS, humanBytes };
export { EventHelpers, UserOperatingSystem, callApi, callJsonApi, formatDate, getCsrfHeader, getUserOS, humanBytes };
//# sourceMappingURL=index.js.map
{
"name": "@internetarchive/ads-library",
"version": "0.0.8",
"version": "0.1.0",
"description": "This is a home for common library and helper functions in use by the Internet Archive's Archiving and Data Services team.",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

import camelcaseKeys from "camelcase-keys";
export function callJsonApi<Rq, Rsp>(options: {
// HTTP request method options represented as a type
export type ApiRequestMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
// base parameters for calling an API - common to all
export interface BaseApiFetchOptions<RequestT> {
url: string;
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
method: ApiRequestMethod;
// user-defined error message on failure
errorMessage: string;
disableCamelize?: boolean;
body?: Rq;
}): Promise<Rsp> {
// request body
body?: RequestT;
}
// Generic type to extend base api fetch options interface
export type ExtendedApiFetchOptions<RequestT, ExtraOptionsT> =
BaseApiFetchOptions<RequestT> & ExtraOptionsT;
// additional options for more flexible api calls
export type ApiFetchOptions<RequestT> = ExtendedApiFetchOptions<
RequestT,
{
// Accept header - what content type does the client / caller expect in return?
acceptType: string;
// Content-Type header - can omit for GET / DELETE / non-payload requests.
contentType?: string;
}
>;
export type JsonApiFetchOptions<RequestT> = ExtendedApiFetchOptions<
RequestT,
{
// all JSON responses are camelized by default due to our Django backend convention
disableCamelize?: boolean;
}
>;
// calls our base callApi method and returns a json type, has option to disable response camel-casing
export function callJsonApi<RequestT, ResponseT>(
options: JsonApiFetchOptions<RequestT>,
): Promise<ResponseT> {
return (
callApi({
...options,
body: options.body ? JSON.stringify(options.body) : undefined,
acceptType: "application/json",
contentType: "application/json",
})
.then((response) => response.json())
// camelize response keys unless disabled, cast to generic response type
.then((json) =>
options.disableCamelize
? (json as ResponseT)
: (camelcaseKeys(json, { deep: true }) as ResponseT),
)
);
}
// generic interface on top of JS fetch function. adds:
// - csrf token
// - friendly interface for accept/content type
// - error handling and logging consistent with ADS patterns
export function callApi<RequestT extends BodyInit | null | undefined>(
options: ApiFetchOptions<RequestT>,
): Promise<Response> {
const request: RequestInit = {

@@ -14,8 +71,13 @@ method: options.method,

...getCsrfHeader(),
Accept: "application/json",
"Content-Type": "application/json",
Accept: options.acceptType,
},
body: options.body,
};
if (options.body) {
request.body = JSON.stringify(options.body);
// to add optional Content-Type, we must reinitialize headers after type refinement
// since HeadersInit doesn't accept optional/mutable types.
if (options.contentType && request.headers) {
request.headers = {
...request.headers,
"Content-Type": options.contentType,
};
}

@@ -26,3 +88,3 @@ return (

if (response.ok) {
return response.json();
return response;
} else {

@@ -32,8 +94,2 @@ throw new Error(options.errorMessage);

})
// camelize response keys unless disabled, cast to generic response type
.then((json) =>
options.disableCamelize
? (json as Rsp)
: (camelcaseKeys(json, { deep: true }) as Rsp),
)
// log error, return no response

@@ -40,0 +96,0 @@ .catch((error) => {

// publicly available library functions
export { EventHelpers } from "./events";
export { callJsonApi, getCsrfHeader } from "./api";
export {
callApi,
callJsonApi,
getCsrfHeader,
ApiFetchOptions,
JsonApiFetchOptions,
ApiRequestMethod,
BaseApiFetchOptions,
ExtendedApiFetchOptions,
} from "./api";
export { UserOperatingSystem, getUserOS } from "./browser";
export { humanBytes, formatDate } from "./formats";

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc