Skip to content

Commit

Permalink
proposing api notation?
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Jan 24, 2024
1 parent 14e231a commit e83a011
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 28 deletions.
20 changes: 0 additions & 20 deletions TEST PLEASE DELETE.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/core/contracts/localizer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

export interface Localizer {
translate(text: string) : string;
translate(text: string, local: string) : string;
}
11 changes: 10 additions & 1 deletion src/core/ioc/dependency-injection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'node:assert';
import type { IntoDependencies } from '../../types/ioc';
import { useContainerRaw } from './base';

Expand Down Expand Up @@ -33,7 +34,9 @@ export function transient<T>(cb: () => () => T) {
*
*/
export function Service<const T extends keyof Dependencies>(key: T) {
return useContainerRaw().get(key)!;
const dep = useContainerRaw().get(key)!;
assert(dep, "Requested key " + key + " returned undefined");
return dep;
}
/**
* @since 3.0.0
Expand All @@ -51,3 +54,9 @@ export function useContainer<const T extends Dependencies>() {
return <V extends (keyof T)[]>(...keys: [...V]) =>
keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as IntoDependencies<V>;
}


export function $local(i: string, local: string) {
return Service('@sern/localizer').translate(i, local)
}

2 changes: 1 addition & 1 deletion src/core/ioc/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { makeDependencies } from './base';
export { Service, Services, single, transient } from './dependency-injection';
export { Service, Services, single, transient, $local } from './dependency-injection';
1 change: 1 addition & 0 deletions src/core/module-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const shouldHandle = (path: string, fpath: string) => {


export type ModuleResult<T> = Promise<ImportPayload<T>>;

/**
* Import any module based on the absolute path.
* This can accept four types of exported modules
Expand Down
7 changes: 6 additions & 1 deletion src/optional/localizer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { Localizer, Init } from '../core/contracts'
import { Localization } from 'shrimple-locales'
import fs from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { join, resolve } from 'node:path';
import { filename } from '../core/module-loading'
import assert from 'node:assert';

/**
* @internal
* @since 2.0.0
Expand All @@ -11,7 +14,8 @@ import { filename } from '../core/module-loading'
export class ShrimpleLocalizer implements Localizer, Init {
private __localization!: Localization;
constructor(){}
translate(text: string): string {
translate(text: string, local: string): string {
this.__localization.changeLanguage(local);
return this.__localization.get(text);
}

Expand All @@ -26,6 +30,7 @@ export class ShrimpleLocalizer implements Localizer, Init {
private async readLocalizationDirectory() {
const translationFiles = [];
const localPath = resolve('locals');
assert(existsSync(localPath), "You need to create a directory called \"locals\" for the localizer")
for(const json of await fs.readdir(localPath)) {
translationFiles.push({ [filename(json)]:
JSON.parse(await fs.readFile(join(localPath, json), 'utf8')) })
Expand Down
2 changes: 1 addition & 1 deletion src/sern.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { handleCrash } from './handlers/_internal';
import callsites from 'callsites';
import { err, ok, Files } from './core/_internal';
import { Files } from './core/_internal';
import { merge } from 'rxjs';
import { Services } from './core/ioc';
import { Wrapper } from './types/core';
Expand Down
1 change: 0 additions & 1 deletion src/types/core-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { AnyCommandPlugin, AnyEventPlugin, ControlPlugin, InitPlugin } from './c
import { Awaitable, Args, SlashOptions, SernEventsMapping } from './utility';



export interface CommandMeta {
fullPath: string;
id: string;
Expand Down
4 changes: 2 additions & 2 deletions src/types/ioc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export type DependencyList = [

export interface CoreDependencies {
'@sern/client': () => Contracts.Emitter;
'@sern/logger'?: () => Contracts.Logging;
'@sern/emitter': () => Contracts.Emitter;
'@sern/store': () => Contracts.CoreModuleStore;
'@sern/modules': () => Contracts.ModuleManager;
'@sern/errors': () => Contracts.ErrorHandling;
'@sern/logger'?: () => Contracts.Logging;
'@sern/localizer'?: () => Contracts.Localizer
}

export type DependencyFromKey<T extends keyof Dependencies> = Dependencies[T];
Expand All @@ -45,7 +46,6 @@ export interface DependencyConfiguration {

//Extra modules that are preconfigured and ready to use! @sern/localizer is an example
include?: string[]

build: (
root: Container<Omit<CoreDependencies, '@sern/client'>, {}>,
) => Container<Dependencies, {}>;
Expand Down

0 comments on commit e83a011

Please sign in to comment.