publicFileMaker function

PartFile publicFileMaker(
  1. ServerServer server,
  2. String formatter(
    1. Spec
    )
)

Implementation

PartFile publicFileMaker(ServerServer server, String Function(Spec) formatter) {
  final publics = Method(
    (p) => p
      ..name = 'public'
      ..lambda = true
      ..type = MethodType.getter
      ..returns = TypeReference(
        (b) => b
          ..symbol = 'List'
          ..types.add(refer((Route).name)),
      )
      ..body = Block.of([
        literalList([
          for (final public in server.public)
            refer((Route).name).newInstance(
              [literalString(public.path)],
              {
                'method': literalString('GET'),
                'allowedOrigins': refer((AllowedOriginsImpl).name)
                    .constInstanceNamed('all', []),
                'ignorePathPattern': literalTrue,
                'handler': Method(
                  (p) => p
                    ..modifier = MethodModifier.async
                    ..requiredParameters.add(
                      Parameter((p) => p..name = 'context'),
                    )
                    ..body = Block.of([
                      refer('context')
                          .property('response')
                          .property('body')
                          .assign(
                            refer((File).name).call([
                              refer('p').property('join').call([
                                literalString('public'),
                                literalString(public.path),
                              ]),
                            ]),
                          )
                          .statement,
                    ]),
                ).closure,
              },
            ),
        ]).statement,
      ]),
  );

  final content = formatter(publics);

  return PartFile(
    path: ['definitions', '__public'],
    content: content,
  );
}