0.0.41 • Published 5 years ago

loopback-connector-file v0.0.41

Weekly downloads
4
License
ISC
Repository
github
Last release
5 years ago

Loopback Connector File

Usage:

file.datasource.json

{
  "name": "filedatasource", // The name you want
  "connector": "loopback-connector-file", // The loopback connector reposituory
  "root": "./storage" // The directory you want to access
}

file.service.ts

export interface FileService {
	getFolder(): Array<string>;
	get(file: string): string;
	overwrite(file: string, data: string): void;
	append(file: string, data: string): void;
	delete(file: string): void;
}

file.provider.ts

import { getService, juggler } from '@loopback/service-proxy';
import { inject, Provider } from '@loopback/core';
import { FileDataSource } from '../datasources/file.datasource';
import { FileService } from '../services/file.service';

export class FileProvider implements Provider<FileService> {
	constructor(
		@inject('datasources.file')
		protected dataSource: juggler.DataSource = new FileDataSource(),
	) { }

	value(): Promise<FileService> {
		return getService(this.dataSource);
	}
}

File Controller example

import { inject } from '@loopback/context';
import { get, requestBody, post, put, RequestBodyObject, param } from "@loopback/rest";

const TEXT_PLAIN_REQUEST: RequestBodyObject = {
	description: '',
	required: true,
	content: {
		'text/plain': {
			schema: {
				type: 'string'

			}
		}
	}

}

export class FileController {
	constructor(
		@inject('file.provider') private fileService: FileService
	) { }

	@get('/files')
	async getFolder() {
		return this.fileService.getFolder();
	}

	@get('/files/{filename}')
	async getFile(
		@param.path.string('filename') filename: string
	) {
		return this.fileService.get(filename);
	}

	@put('/files/{filename}')
	async putFile(
		@param.path.string('filename') filename: string,
		@requestBody(TEXT_PLAIN_REQUEST) body: string
	) {
		return this.fileService.overwrite(filename, body);
	}

	@post('/files/{filename}')
	async postFile(
		@param.path.string('filename') filename: string,
		@requestBody(TEXT_PLAIN_REQUEST) body: string
	) {
		return this.fileService.append(filename, body);
	}
}

Do not use on production, this is test.

0.0.41

5 years ago

0.0.40

5 years ago

0.0.39

5 years ago

0.0.38

5 years ago

0.0.37

5 years ago

0.0.36

5 years ago

0.0.35

5 years ago

0.0.33

5 years ago

0.0.32

5 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago