Skip to content

Instantly share code, notes, and snippets.

@jakubfijalkowski
Created January 18, 2021 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakubfijalkowski/8094473baa8a906fbfa0a59d9de1996d to your computer and use it in GitHub Desktop.
Save jakubfijalkowski/8094473baa8a906fbfa0a59d9de1996d to your computer and use it in GitHub Desktop.
LeanCode.Example Contracts
namespace LeanCode.Example.Core.Contracts
{
public static class Auth
{
public static class Roles
{
public const string User = "user";
}
public static class KnownClaims
{
public const string UserId = "sub";
public const string Role = "role";
}
public static class Clients
{
public const string AdminApp = "admin_app";
public const string ClientApp = "client_app";
}
public static class Scopes
{
public const string InternalApi = "internal_api";
}
}
}
[
{
"RootPath": "./",
"OutPath": "./contracts",
"ContractsRegex": "LeanCode.Example.Core.Contracts(/|\\\\).*\\.cs$",
"Name": "Example",
"TypeScript": {
"ContractsPreambleLines": [
"/* eslint-disable */",
"import { CqrsClient as CQRS, RemoteCommand as IRemoteCommand, RemoteQuery as IRemoteQuery } from \"definitions/cqrs\";",
"interface AuthorizeWhenAttribute { }",
"interface Enum { }",
""
],
"TypeTranslations": {
"int": "number",
"double": "number",
"float": "number",
"single": "number",
"int32": "number",
"uint32": "number",
"byte": "number",
"sbyte": "number",
"int64": "number",
"short": "number",
"long": "number",
"decimal": "number",
"bool": "boolean",
"boolean": "boolean",
"date": "string",
"datetime": "ApiDateTime",
"timespan": "ApiTimeSpan",
"datetimeoffset": "ApiDateTime",
"guid": "string",
"string": "string",
"uri": "string",
"jobject": "any",
"dynamic": "any",
"object": "any"
}
},
"Dart": {
"ContractsPreambleLines": [
"import 'package:cqrs/contracts.dart';",
"import 'package:json_annotation/json_annotation.dart';",
"",
"part 'contracts.g.dart';",
"",
"abstract class AuthorizeWhenAttribute {}",
""
],
"TypeTranslations": {
"int": "int",
"double": "double",
"float": "double",
"single": "double",
"int32": "int",
"uint32": "int",
"byte": "int",
"sbyte": "int",
"int64": "int",
"short": "int",
"long": "int",
"decimal": "double",
"bool": "bool",
"boolean": "bool",
"datetime": "DateTime",
"date": "DateTime",
"timespan": "String",
"datetimeoffset": "DateTime",
"guid": "String",
"string": "String",
"jobject": "dynamic",
"dynamic": "dynamic",
"object": "Object"
}
}
}
]
import 'package:cqrs/contracts.dart';
import 'package:json_annotation/json_annotation.dart';
part 'contracts.g.dart';
abstract class AuthorizeWhenAttribute {}
List<T> _listFromJson<T>(Iterable<dynamic> decodedJson, T itemFromJson(Map<String, dynamic> map)) {
return decodedJson
?.map((dynamic e) => itemFromJson(e as Map<String,dynamic>))
?.toList()
?.cast<T>();
}
DateTime _dateTimeFromJson(String value) {
return DateTime.parse('${value.substring(0, 19)} Z');
}
DateTime _nullableDateTimeFromJson(String value) {
return value == null ? null : _dateTimeFromJson(value);
}
double _doubleFromJson(dynamic value) {
if (value is double) { return value; }
else if (value is String) { return double.parse(value); }
else if (value is int) { return value.toDouble(); }
else { throw Exception('Invalid argument type ${value.runtimeType}'); }
}
double _nullableDoubleFromJson(dynamic value) {
return value == null ? null : _doubleFromJson(value);
}
@JsonSerializable()
class Auth {
Auth();
factory Auth.fromJson(Map<String, dynamic> json) => _$AuthFromJson(json);
Map<String, dynamic> toJson() => _$AuthToJson(this);
}
@JsonSerializable()
class Roles {
Roles();
factory Roles.fromJson(Map<String, dynamic> json) => _$RolesFromJson(json);
static const user = "user";
Map<String, dynamic> toJson() => _$RolesToJson(this);
}
@JsonSerializable()
class KnownClaims {
KnownClaims();
factory KnownClaims.fromJson(Map<String, dynamic> json) => _$KnownClaimsFromJson(json);
static const userId = "sub";
static const role = "role";
Map<String, dynamic> toJson() => _$KnownClaimsToJson(this);
}
@JsonSerializable()
class Clients {
Clients();
factory Clients.fromJson(Map<String, dynamic> json) => _$ClientsFromJson(json);
static const adminApp = "admin_app";
static const clientApp = "client_app";
Map<String, dynamic> toJson() => _$ClientsToJson(this);
}
@JsonSerializable()
class Scopes {
Scopes();
factory Scopes.fromJson(Map<String, dynamic> json) => _$ScopesFromJson(json);
static const internalApi = "internal_api";
Map<String, dynamic> toJson() => _$ScopesToJson(this);
}
@JsonSerializable()
class ExampleCommand implements IRemoteCommand {
ExampleCommand();
factory ExampleCommand.fromJson(Map<String, dynamic> json) => _$ExampleCommandFromJson(json);
@JsonKey(name: 'Arg')
String arg;
@override
String getFullName() => 'LeanCode.Example.Core.Contracts.Example.ExampleCommand';
@override
Map<String, dynamic> toJson() => _$ExampleCommandToJson(this);
}
@JsonSerializable()
class ErrorCodes {
ErrorCodes();
factory ErrorCodes.fromJson(Map<String, dynamic> json) => _$ErrorCodesFromJson(json);
static const emptyArg = 1;
Map<String, dynamic> toJson() => _$ErrorCodesToJson(this);
}
/* eslint-disable */
import { CqrsClient as CQRS, RemoteCommand as IRemoteCommand, RemoteQuery as IRemoteQuery } from "definitions/cqrs";
interface AuthorizeWhenAttribute { }
interface Enum { }
export interface ExampleCommand extends IRemoteCommand {
Arg: string;
}
export const Auth = {
Roles: {
User: "user",
},
KnownClaims: {
UserId: "sub",
Role: "role",
},
Clients: {
AdminApp: "admin_app",
ClientApp: "client_app",
},
Scopes: {
InternalApi: "internal_api",
},
};
export const ExampleCommand = {
ErrorCodes: {
EmptyArg: 1,
},
};
export default function (cqrsClient: CQRS) {
return {
exampleCommand: (dto: ExampleCommand) => cqrsClient.executeCommand<typeof ExampleCommand["ErrorCodes"]>("LeanCode.Example.Core.Contracts.Example.ExampleCommand", dto),
};
}
using LeanCode.CQRS;
using LeanCode.CQRS.Security;
namespace LeanCode.Example.Core.Contracts.Example
{
[AllowUnauthorized]
public class ExampleCommand : IRemoteCommand
{
public string Arg { get; set; }
public static class ErrorCodes
{
public const int EmptyArg = 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment