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

piral-ext

Package Overview
Dependencies
Maintainers
1
Versions
1021
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

piral-ext - npm Package Compare versions

Comparing version 0.6.0-pre.496 to 0.6.0-pre.524

src/fetch/create.test.ts

16

lib/fetch/create.js

@@ -12,10 +12,16 @@ "use strict";

fetch(path, options = {}) {
const { method = 'get', body, headers = {}, cache = baseInit.cache, mode = baseInit.mode } = options;
const ct = 'content-type';
const { method = 'get', body, headers = {}, cache = baseInit.cache, mode = baseInit.mode, result = 'auto', } = options;
const json = Array.isArray(body) ||
typeof body === 'number' ||
(typeof body === 'object' && body instanceof FormData === false && body instanceof Blob === false);
const url = new URL(path, baseUrl);
const init = Object.assign({}, baseInit, { method,
body, headers: Object.assign({}, baseHeaders, headers), cache,
const init = Object.assign({}, baseInit, { method, body: json ? JSON.stringify(body) : body, headers: Object.assign({}, baseHeaders, headers), cache,
mode });
if (json) {
init.headers[ct] = 'application/json';
}
return fetch(url.href, init).then(res => {
const contentType = res.headers.get('content-type');
const json = contentType.indexOf('json') !== -1;
const contentType = res.headers.get(ct);
const json = result === 'json' || (result === 'auto' && contentType.indexOf('json') !== -1);
const promise = json ? res.json() : res.text();

@@ -22,0 +28,0 @@ return promise.then(body => ({

@@ -11,3 +11,3 @@ export interface FetchOptions {

*/
body?: string | Blob | FormData;
body?: string | Blob | FormData | Array<any> | {} | number;
/**

@@ -26,2 +26,7 @@ * Sets the headers of the request.

mode?: RequestMode;
/**
* Sets the result mode of the request.
* @default 'auto'
*/
result?: 'auto' | 'json' | 'text';
}

@@ -54,2 +59,5 @@ export interface FetchResponse<T> {

}
export interface PiralFetchApiFetch {
<T = any>(url: string, options?: FetchOptions): Promise<FetchResponse<T>>;
}
export interface PiralFetchApi {

@@ -61,3 +69,3 @@ /**

*/
fetch<T = any>(url: string, options?: FetchOptions): Promise<FetchResponse<T>>;
fetch: PiralFetchApiFetch;
}

@@ -69,2 +69,11 @@ export interface GqlUnsubscriber {

}
export interface PiralGqlApiQuery {
<T = any>(query: string, options?: GqlQueryOptions): Promise<T>;
}
export interface PiralGqlApiMutate {
<T = any>(mutation: string, options?: GqlMutationOptions): Promise<T>;
}
export interface PiralGqlApiSubscribe {
<T = any>(subscription: string, subscriber: GqlSubscriber<T>, options?: GqlSubscriptionOptions): GqlUnsubscriber;
}
export interface PiralGqlApi {

@@ -76,3 +85,3 @@ /**

*/
query<T = any>(query: string, options?: GqlQueryOptions): Promise<T>;
query: PiralGqlApiQuery;
/**

@@ -83,3 +92,3 @@ * Executes the given GraphQL mutation.

*/
mutate<T = any>(mutation: string, options?: GqlMutationOptions): Promise<T>;
mutate: PiralGqlApiMutate;
/**

@@ -91,3 +100,3 @@ * Establishes the given GraphQL subscription.

*/
subscribe<T = any>(subscription: string, subscriber: GqlSubscriber<T>, options?: GqlSubscriptionOptions): GqlUnsubscriber;
subscribe: PiralGqlApiSubscribe;
}

@@ -0,3 +1,3 @@

import { Localizer } from './localize';
import { PiralLocaleApi, LocaleConfig } from './types';
import { Localizer } from './localize';
/**

@@ -4,0 +4,0 @@ * Sets up a new localizer by using the given config.

@@ -9,3 +9,5 @@ "use strict";

function setupLocalizer(config = {}) {
return new localize_1.Localizer(config.messages || {});
const msgs = config.messages || {};
const lang = config.language || Object.keys(msgs)[0] || 'en';
return new localize_1.Localizer(msgs, lang, config.fallback, config.load);
}

@@ -12,0 +14,0 @@ exports.setupLocalizer = setupLocalizer;

@@ -1,9 +0,22 @@

import { LocalizationMessages } from './types';
import { LocalizationMessages, TranslationLoader } from './types';
declare function defaultFallback(key: string, language: string): string;
export declare class Localizer {
private globalMessages;
private language;
private fallback;
private load?;
/**
* Creates a new instance of a localizer.
*/
constructor(globalMessages: LocalizationMessages);
constructor(globalMessages: LocalizationMessages, language: string, fallback?: typeof defaultFallback, load?: TranslationLoader);
/**
* Gets the currently set language.
*/
readonly currentLanguage: string;
/**
* Changes the currently set language.
* @param language The language to change to.
*/
changeLanguage(language: string): void;
/**
* Localizes the given key via the global translations.

@@ -22,2 +35,4 @@ * @param key The key of the translation snippet.

localizeLocal<T>(localMessages: LocalizationMessages, key: string, variables?: T): string;
private localizeBase;
}
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const piral_core_1 = require("piral-core");
function getMissingKeyString(key, language) {
function defaultFallback(key, language) {
return `__${language}_${key}__`;

@@ -17,9 +16,2 @@ }

}
function localizeBase(messages, language, key, variables) {
const message = translateMessage(language, messages, key, variables);
if (message === undefined) {
return getMissingKeyString(key, language);
}
return message;
}
class Localizer {

@@ -29,6 +21,29 @@ /**

*/
constructor(globalMessages) {
constructor(globalMessages, language, fallback = defaultFallback, load) {
this.globalMessages = globalMessages;
this.language = language;
this.fallback = fallback;
this.load = load;
}
/**
* Gets the currently set language.
*/
get currentLanguage() {
return this.language;
}
/**
* Changes the currently set language.
* @param language The language to change to.
*/
changeLanguage(language) {
if (this.language !== language) {
this.language = language;
if (typeof this.load === 'function') {
this.load(language).then(translations => {
this.globalMessages[language] = translations;
});
}
}
}
/**
* Localizes the given key via the global translations.

@@ -39,5 +54,5 @@ * @param key The key of the translation snippet.

localizeGlobal(key, variables) {
const language = piral_core_1.useGlobalState(m => m.app.language.selected);
const language = this.language;
const messages = this.globalMessages;
return localizeBase(messages, language, key, variables);
return this.localizeBase(messages, language, key, variables);
}

@@ -52,11 +67,18 @@ /**

localizeLocal(localMessages, key, variables) {
const language = piral_core_1.useGlobalState(m => m.app.language.selected);
const language = this.language;
const message = translateMessage(language, localMessages, key, variables);
if (message === undefined) {
return localizeBase(this.globalMessages, language, key, variables);
return this.localizeBase(this.globalMessages, language, key, variables);
}
return message;
}
localizeBase(messages, language, key, variables) {
const message = translateMessage(language, messages, key, variables);
if (message === undefined) {
return this.fallback(key, language);
}
return message;
}
}
exports.Localizer = Localizer;
//# sourceMappingURL=localize.js.map

@@ -0,1 +1,7 @@

export interface Translations {
/**
* The available wordings (tag to translation).
*/
[tag: string]: string;
}
export interface LocalizationMessages {

@@ -5,9 +11,10 @@ /**

*/
[lang: string]: {
/**
* The available wordings (tag to translation).
*/
[tag: string]: string;
};
[lang: string]: Translations;
}
export interface TranslationLoader {
(language: string): Promise<Translations>;
}
export interface TranslationFallback {
(key: string, language: string): string;
}
export interface PiralLocaleApi {

@@ -34,2 +41,14 @@ /**

messages?: LocalizationMessages;
/**
* Sets the default language to use.
*/
language?: string;
/**
* Sets the default language to use.
*/
load?: TranslationLoader;
/**
* Sets the optional fallback to use.
*/
fallback?: TranslationFallback;
}
{
"name": "piral-ext",
"version": "0.6.0-pre.496",
"version": "0.6.0-pre.524",
"description": "Useful API extensions and component definitions for extending piral-core.",

@@ -36,3 +36,3 @@ "keywords": [

"devDependencies": {
"piral-core": "^0.6.0-pre.496"
"piral-core": "^0.6.0-pre.524"
},

@@ -49,3 +49,3 @@ "peerDependencies": {

},
"gitHead": "f08e7a9972681f05dd29e59351fe6b5c250e5504"
"gitHead": "edc94f2987d634a434ff9787d4cfffacca2276cf"
}

@@ -13,3 +13,15 @@ import { PiralFetchApi, FetchConfig } from './types';

fetch(path, options = {}) {
const { method = 'get', body, headers = {}, cache = baseInit.cache, mode = baseInit.mode } = options;
const ct = 'content-type';
const {
method = 'get',
body,
headers = {},
cache = baseInit.cache,
mode = baseInit.mode,
result = 'auto',
} = options;
const json =
Array.isArray(body) ||
typeof body === 'number' ||
(typeof body === 'object' && body instanceof FormData === false && body instanceof Blob === false);
const url = new URL(path, baseUrl);

@@ -19,3 +31,3 @@ const init: RequestInit = {

method,
body,
body: json ? JSON.stringify(body) : (body as BodyInit),
headers: {

@@ -28,5 +40,10 @@ ...baseHeaders,

};
if (json) {
init.headers[ct] = 'application/json';
}
return fetch(url.href, init).then(res => {
const contentType = res.headers.get('content-type');
const json = contentType.indexOf('json') !== -1;
const contentType = res.headers.get(ct);
const json = result === 'json' || (result === 'auto' && contentType.indexOf('json') !== -1);
const promise = json ? res.json() : res.text();

@@ -33,0 +50,0 @@

@@ -11,3 +11,3 @@ export interface FetchOptions {

*/
body?: string | Blob | FormData;
body?: string | Blob | FormData | Array<any> | {} | number;
/**

@@ -26,2 +26,7 @@ * Sets the headers of the request.

mode?: RequestMode;
/**
* Sets the result mode of the request.
* @default 'auto'
*/
result?: 'auto' | 'json' | 'text';
}

@@ -57,2 +62,6 @@

export interface PiralFetchApiFetch {
<T = any>(url: string, options?: FetchOptions): Promise<FetchResponse<T>>;
}
export interface PiralFetchApi {

@@ -64,3 +73,3 @@ /**

*/
fetch<T = any>(url: string, options?: FetchOptions): Promise<FetchResponse<T>>;
fetch: PiralFetchApiFetch;
}

@@ -76,2 +76,14 @@ export interface GqlUnsubscriber {

export interface PiralGqlApiQuery {
<T = any>(query: string, options?: GqlQueryOptions): Promise<T>;
}
export interface PiralGqlApiMutate {
<T = any>(mutation: string, options?: GqlMutationOptions): Promise<T>;
}
export interface PiralGqlApiSubscribe {
<T = any>(subscription: string, subscriber: GqlSubscriber<T>, options?: GqlSubscriptionOptions): GqlUnsubscriber;
}
export interface PiralGqlApi {

@@ -83,3 +95,3 @@ /**

*/
query<T = any>(query: string, options?: GqlQueryOptions): Promise<T>;
query: PiralGqlApiQuery;
/**

@@ -90,3 +102,3 @@ * Executes the given GraphQL mutation.

*/
mutate<T = any>(mutation: string, options?: GqlMutationOptions): Promise<T>;
mutate: PiralGqlApiMutate;
/**

@@ -98,7 +110,3 @@ * Establishes the given GraphQL subscription.

*/
subscribe<T = any>(
subscription: string,
subscriber: GqlSubscriber<T>,
options?: GqlSubscriptionOptions,
): GqlUnsubscriber;
subscribe: PiralGqlApiSubscribe;
}

@@ -1,26 +0,14 @@

import * as piralCore from 'piral-core';
import { createLocaleApi, setupLocalizer } from './create';
jest.mock('piral-core');
const config = {
messages: {
fr: {
foo: 'bár',
bar: 'bár',
},
},
};
describe('Create Localize API', () => {
it('createApi can translate from global translations using the current language', () => {
piralCore.useGlobalState = jest.fn(f =>
f({
app: {
language: {
selected: 'fr',
},
const config = {
language: 'fr',
messages: {
fr: {
foo: 'bár',
bar: 'bár',
},
}),
);
},
};
const api = createLocaleApi(setupLocalizer(config));

@@ -32,11 +20,11 @@ const result = api.translate('foo');

it('createApi can translate from local translations using the current language', () => {
piralCore.useGlobalState = jest.fn(f =>
f({
app: {
language: {
selected: 'fr',
},
const config = {
language: 'fr',
messages: {
fr: {
foo: 'bár',
bar: 'bár',
},
}),
);
},
};
const api = createLocaleApi(setupLocalizer(config));

@@ -53,11 +41,11 @@ api.provideTranslations({

it('createApi can translate from local-global translations using the current language', () => {
piralCore.useGlobalState = jest.fn(f =>
f({
app: {
language: {
selected: 'fr',
},
const config = {
language: 'fr',
messages: {
fr: {
foo: 'bár',
bar: 'bár',
},
}),
);
},
};
const api = createLocaleApi(setupLocalizer(config));

@@ -74,11 +62,11 @@ api.provideTranslations({

it('createApi falls back to standard string if language is not found', () => {
piralCore.useGlobalState = jest.fn(f =>
f({
app: {
language: {
selected: 'en',
},
const config = {
language: 'en',
messages: {
fr: {
foo: 'bár',
bar: 'bár',
},
}),
);
},
};
const api = createLocaleApi(setupLocalizer(config));

@@ -90,11 +78,11 @@ const result = api.translate('bar');

it('createApi falls back to standard string if key is not found', () => {
piralCore.useGlobalState = jest.fn(f =>
f({
app: {
language: {
selected: 'fr',
},
const config = {
language: 'fr',
messages: {
fr: {
foo: 'bár',
bar: 'bár',
},
}),
);
},
};
const api = createLocaleApi(setupLocalizer(config));

@@ -101,0 +89,0 @@ const result = api.translate('qxz');

@@ -0,3 +1,3 @@

import { Localizer } from './localize';
import { PiralLocaleApi, LocalizationMessages, LocaleConfig } from './types';
import { Localizer } from './localize';

@@ -9,3 +9,5 @@ /**

export function setupLocalizer(config: LocaleConfig = {}) {
return new Localizer(config.messages || {});
const msgs = config.messages || {};
const lang = config.language || Object.keys(msgs)[0] || 'en';
return new Localizer(msgs, lang, config.fallback, config.load);
}

@@ -12,0 +14,0 @@

@@ -1,15 +0,3 @@

import * as piralCore from 'piral-core';
import { Localizer } from './localize';
jest.mock('piral-core');
piralCore.useGlobalState = (select: any) =>
select({
app: {
language: {
selected: 'en',
},
},
});
const messages = {

@@ -27,3 +15,3 @@ en: {

it('localizeLocal translates from the local translations if available', () => {
const localizer = new Localizer(messages);
const localizer = new Localizer(messages, 'en');
const result = localizer.localizeLocal(

@@ -41,3 +29,3 @@ {

it('localizeLocal translates from the global translations if local not available', () => {
const localizer = new Localizer(messages);
const localizer = new Localizer(messages, 'en');
const result = localizer.localizeLocal(

@@ -55,3 +43,3 @@ {

it('localizeGlobal translates from the global translations', () => {
const localizer = new Localizer(messages);
const localizer = new Localizer(messages, 'en');
const result = localizer.localizeGlobal('hi');

@@ -62,3 +50,3 @@ expect(result).toBe('hello');

it('localizeGlobal translates with variable interpolation', () => {
const localizer = new Localizer(messages);
const localizer = new Localizer(messages, 'en');
const result = localizer.localizeGlobal('greeting', { name: 'User' });

@@ -69,3 +57,3 @@ expect(result).toBe('Hi User, welcome back');

it('localizeGlobal variable interpolation ignores non-used variables', () => {
const localizer = new Localizer(messages);
const localizer = new Localizer(messages, 'en');
const result = localizer.localizeGlobal('greeting', { name: 'User', age: 99 });

@@ -76,3 +64,3 @@ expect(result).toBe('Hi User, welcome back');

it('localizeGlobal ignores non-available variables', () => {
const localizer = new Localizer(messages);
const localizer = new Localizer(messages, 'en');
const result = localizer.localizeGlobal('greeting', { nom: 'User' });

@@ -83,3 +71,3 @@ expect(result).toBe('Hi {{name}}, welcome back');

it('localizeGlobal places missing string placeholder if not found', () => {
const localizer = new Localizer(messages);
const localizer = new Localizer(messages, 'en');
const result = localizer.localizeGlobal('ho');

@@ -90,3 +78,3 @@ expect(result).toBe('__en_ho__');

it('localizeGlobal replaces undefined variables with an empty string', () => {
const localizer = new Localizer(messages);
const localizer = new Localizer(messages, 'en');
const result = localizer.localizeGlobal('greeting', { name: undefined });

@@ -93,0 +81,0 @@ expect(result).toBe('Hi , welcome back');

@@ -1,5 +0,4 @@

import { useGlobalState } from 'piral-core';
import { LocalizationMessages } from './types';
import { LocalizationMessages, TranslationLoader } from './types';
function getMissingKeyString(key: string, language: string): string {
function defaultFallback(key: string, language: string): string {
return `__${language}_${key}__`;

@@ -20,18 +19,36 @@ }

function localizeBase<T>(messages: LocalizationMessages, language: string, key: string, variables?: T) {
const message = translateMessage(language, messages, key, variables);
export class Localizer {
/**
* Creates a new instance of a localizer.
*/
constructor(
private globalMessages: LocalizationMessages,
private language: string,
private fallback = defaultFallback,
private load?: TranslationLoader,
) {}
if (message === undefined) {
return getMissingKeyString(key, language);
/**
* Gets the currently set language.
*/
public get currentLanguage(): string {
return this.language;
}
return message;
}
export class Localizer {
/**
* Creates a new instance of a localizer.
* Changes the currently set language.
* @param language The language to change to.
*/
constructor(private globalMessages: LocalizationMessages) {}
public changeLanguage(language: string) {
if (this.language !== language) {
this.language = language;
if (typeof this.load === 'function') {
this.load(language).then(translations => {
this.globalMessages[language] = translations;
});
}
}
}
/**

@@ -43,5 +60,5 @@ * Localizes the given key via the global translations.

public localizeGlobal<T>(key: string, variables?: T) {
const language = useGlobalState(m => m.app.language.selected);
const language = this.language;
const messages = this.globalMessages;
return localizeBase(messages, language, key, variables);
return this.localizeBase(messages, language, key, variables);
}

@@ -57,7 +74,7 @@

public localizeLocal<T>(localMessages: LocalizationMessages, key: string, variables?: T) {
const language = useGlobalState(m => m.app.language.selected);
const language = this.language;
const message = translateMessage(language, localMessages, key, variables);
if (message === undefined) {
return localizeBase(this.globalMessages, language, key, variables);
return this.localizeBase(this.globalMessages, language, key, variables);
}

@@ -67,2 +84,12 @@

}
private localizeBase<T>(messages: LocalizationMessages, language: string, key: string, variables?: T) {
const message = translateMessage(language, messages, key, variables);
if (message === undefined) {
return this.fallback(key, language);
}
return message;
}
}

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

export interface Translations {
/**
* The available wordings (tag to translation).
*/
[tag: string]: string;
}
export interface LocalizationMessages {

@@ -5,10 +12,13 @@ /**

*/
[lang: string]: {
/**
* The available wordings (tag to translation).
*/
[tag: string]: string;
};
[lang: string]: Translations;
}
export interface TranslationLoader {
(language: string): Promise<Translations>;
}
export interface TranslationFallback {
(key: string, language: string): string;
}
export interface PiralLocaleApi {

@@ -36,2 +46,14 @@ /**

messages?: LocalizationMessages;
/**
* Sets the default language to use.
*/
language?: string;
/**
* Sets the default language to use.
*/
load?: TranslationLoader;
/**
* Sets the optional fallback to use.
*/
fallback?: TranslationFallback;
}

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